{"id":263394,"date":"2015-08-12T13:23:02","date_gmt":"2015-08-12T09:23:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=263394"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=263394","title":{"rendered":"Live \u2014 \u0447\u0430\u0442 \u0438 ActionCable"},"content":{"rendered":"<p>     \t\u0425\u043e\u0447\u0443 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u0437\u0430\u043c\u0435\u0442\u043a\u0443 \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0440\u0435\u043b\u0438\u0437\u0438\u043d\u044b\u0439 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u0434\u0430\u0432\u043d\u043e, ActionCable \u0432 \u0432\u0430\u0448 Ruby on Rails \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 Live \u2014 \u0447\u0430\u0442\u0430. \u042f \u043d\u0435 \u0441\u0442\u0430\u043d\u0443 \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u0442\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u0430\u043c <a href=\"https:\/\/github.com\/rails\/actioncable\">ActionCable<\/a>, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<a name=\"habracut\"><\/a><\/p>\n<h4>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h4>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 <i>Gemfile<\/i> actioncable \u0438 puma:<\/p>\n<pre><code class=\"ruby\">gem 'actioncable', github: 'rails\/actioncable' gem 'puma' <\/code><\/pre>\n<p>  ActionCable \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0439 web-\u0441\u0435\u0440\u0432\u0435\u0440 <a href=\"https:\/\/github.com\/puma\/puma\">Puma<\/a>.<\/p>\n<h4> \u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0443\u0442\u0440\u0430 \u0447\u0430\u0442\u0430<\/h4>\n<p>  \u041d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043d\u0438\u043a, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043c\u044b \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0435\u0433\u043e \u0432 \u043a\u0443\u043a\u0438 \u0438 \u0441\u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0438\u043c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0447\u0430\u0442\u0430.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u0443 \u0440\u043e\u0443\u0442\u043e\u0432 \u0432 <i>config\/routes.rb<\/i>:<\/p>\n<pre><code class=\"ruby\">resources :messages, only: [:index, :create] resources :sessions, only: [:new, :create] <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0441 \u044d\u043a\u0448\u0435\u043d\u0430\u043c\u0438. \u0417\u0430\u043f\u0438\u0441\u044c \u043d\u0438\u043a\u0430 \u0432 \u043a\u0443\u043a\u0438:<\/p>\n<pre><code class=\"ruby\"># app\/controllers\/sessions_controller.rb class SessionsController &lt; ApplicationController   def create     cookies.signed[:username] = params[:session][:username]     redirect_to messages_path   end end <\/code><\/pre>\n<p>  \u0424\u043e\u0440\u043c\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f app\/views\/sessions\/new.html.slim:<\/p>\n<pre><code class=\"ruby\">= form_for :session, url: sessions_path do |f|   p     = f.label :username, '\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0412\u0430\u0448 \u043d\u0438\u043a'   p     = f.text_field :username   p     = f.submit '\u041e\u043a' <\/code><\/pre>\n<p>  \u0412 \u0441\u0430\u043c\u043e\u043c \u0447\u0430\u0442\u0435 \u043f\u043e\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c 200 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"ruby\"># app\/controllers\/messages_controller.rb class MessagesController &lt; ApplicationController   def create     head :ok   end end <\/code><\/pre>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 app\/views\/messages\/index.html.slim:<\/p>\n<pre><code class=\"ruby\">p   = cookies.signed[:username] p   #messages p   = form_for :message, url: messages_path, remote: true, id: 'messages-form' do |f|     p       = f.label :body, '\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435:'     p       = f.text_field :body     p       = f.submit '\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c' <\/code><\/pre>\n<p>  \u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f AJAX&#8217;\u043e\u043c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440.<\/p>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 ActionCalbe<\/h4>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b \u0437\u0430 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441 ActionCable.<\/p>\n<pre><code class=\"ruby\"># app\/channels\/application_cable\/connection.rb module ApplicationCable   class Connection &lt; ActionCable::Connection::Base   end end   # app\/channels\/application_cable\/channel.rb module ApplicationCable   class Channel &lt; ActionCable::Channel::Base   end end   # app\/channels\/messages_channel.rb class MessagesChannel &lt; ApplicationCable::Channel   def subscribed     stream_from 'messages'   end end <\/code><\/pre>\n<p>  \u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u0432 <i>autoload_paths<\/i>. \u0422\u0435\u043f\u0435\u0440\u044c, \u0432\u0441\u0435, \u043a\u0442\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u043a\u0430\u043d\u0430\u043b <i>MessagesChannel<\/i>, \u0441\u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u043c\u0443 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <i>subscribed<\/i>, \u0442.\u0435. \u2014 <i>messages<\/i>.<\/p>\n<p>  ActionCable \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043e\u0431\u043c\u0435\u043d \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 Redis. \u0411\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u043d \u0443 \u0412\u0430\u0441 \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d. \u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u044c \u0441 \u043d\u0438\u043c \u0432 <i>config\/redis\/cable.yml<\/i>:<\/p>\n<pre><code class=\"ruby\">development: &dev   :url: redis:\/\/localhost:6379   :host: localhost   :port: 6379   :timeout: 1   :inline: true test: *dev production: *dev <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c Puma:<\/p>\n<pre><code class=\"ruby\"># cable\/config.ru require ::File.expand_path('..\/..\/config\/environment',  __FILE__) Rails.application.eager_load!   require 'action_cable\/process\/logging'   run ActionCable.server <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u0443\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u043f\u043e\u0440\u0442\u0443 34523, \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u0438 \u043d\u0435\u0442\u2026<\/p>\n<pre><code class=\"ruby\"># \/bin\/bash bundle exec puma -p 34523 cable\/config.ru <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0443\u043c\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043c\u0430\u043d\u0434\u043e\u0439 <i>.\/bin\/cable<\/i>.<\/p>\n<h4>\u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0432 \u043e\u0431\u0449\u0438\u0439 \u0447\u0430\u0442<\/h4>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u043f\u0440\u0430\u0432\u0438\u043c <i>app\/controllers\/messages_controller.rb<\/i>:  <\/p>\n<pre><code class=\"ruby\">class MessagesController &lt; ApplicationController   def create     ActionCable.server.broadcast 'messages',       message: params[:message][:body],       username: cookies.signed[:username]       head :ok   end end <\/code><\/pre>\n<p>  \u0421\u0430\u043c\u043e\u0435 \u0432\u0430\u0436\u043d\u043e\u0435 \u2014 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0430 \u043d\u0430 \u043a\u0430\u043d\u0430\u043b. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0432\u0430 <i>coffee<\/i> \u0444\u0430\u0439\u043b\u0430 (\u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u0440\u043e\u0440\u0435\u043a\u0432\u0430\u0439\u0440\u0438\u0442\u044c \u0438\u0445):<\/p>\n<pre><code class=\"ruby\">#app\/assets\/javascripts\/channels\/messages.coffee   App.messages = App.cable.subscriptions.create 'MessagesChannel',   received: (data) -&gt;     $('#messages').append(&quot;&lt;p&gt;&lt;b&gt;[#{data.username}]:&lt;\/b&gt; #{data.message}&lt;\/p&gt;&quot;)     #app\/assets\/javascripts\/channels\/index.coffee   #= require cable #= require_self #= require_tree .   @App = {} App.cable = Cable.createConsumer('ws:\/\/127.0.0.1:34523') <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0451.<br \/>  \u041a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u2014 <a href=\"https:\/\/github.com\/lon10\/live-chat\">https:\/\/github.com\/lon10\/live-chat<\/a>     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/264589\/\"> http:\/\/habrahabr.ru\/post\/264589\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u0425\u043e\u0447\u0443 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u0437\u0430\u043c\u0435\u0442\u043a\u0443 \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0440\u0435\u043b\u0438\u0437\u0438\u043d\u044b\u0439 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u0434\u0430\u0432\u043d\u043e, ActionCable \u0432 \u0432\u0430\u0448 Ruby on Rails \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 Live \u2014 \u0447\u0430\u0442\u0430. \u042f \u043d\u0435 \u0441\u0442\u0430\u043d\u0443 \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u0442\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u0430\u043c <a href=\"https:\/\/github.com\/rails\/actioncable\">ActionCable<\/a>, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-263394","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/263394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=263394"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/263394\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=263394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=263394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=263394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}