rails5 ActionCable in chatroom, 为之后开发狼人多人游戏做准备
rails new chatand
bundle install, we add some related in Gemfile, so now it will look like this:
 in routes.rb, we set the root page to be
which means that it will first find the SessionsController’s new method and implement it. We know in rails that when it runs a controller method, it will also render the corresponding views html.erb in view folder. Thus the
views/sessions/new.html.erb will be first rendered.
Further observing in routes.rb:
resources :messages, only: [:index, :create]
resources :sessions, only: [:new, :create]
we find the use of
resources, which means we treat those objects as RESTFUL as we could, those objects in their controllers will have all seven methods like new, create, update, destroy and so on which corresponds to the Http Verbs like post, get and so on.
Then, we look at the new.html.erb
<%= form_for :session, url: sessions_path do |f| %>
:session is the name for this form, when in use in SessionsController, we obtain the information in this form by
cookies.signed[:username] = params[:session][:username]
url: sessions_path is for directing the action to SessionsController#create.
we use index to render the view
Signed in as @<%= cookies.signed[:username] %>.
<%= form_for :message, url: messages_path, remote: true, id: messages-form do |f| %>
<%= f.label :body, 'Enter a message:' %> <br>
<%= f.text_field :body %> <br>
<%= f.submit 'Send message' %>
<% end %>
where the cookies are carrying the parameters we need in another controller, while the empty
div will eventually be filled with previous content. As for the form, it will pretty similar as the SessionsController#new.
- here we use
remote: true, which is calling with Ajax. In usual case, we will need to specify the partial js file we want to implement in MessagesContrller#create method. * But, here we just want it to reload again without refreshing the page.
ActionCable uses Redis for publishihng and subscribing to the messages, so we need to configure Redis in config/redis/cable.yml. You will need Redis installed and running for ActionCable to work.
reds is a key-value store, often referred to as a NoSQL database. Key features are that:  the data can be retrieved by key later in use.  the key-value will be stored permanently.
SET server:name “fido”
x = GET count
x = x + 1
SET count x
we choose to use INCR because it is a atomic operation which will work as expected if multiple users want to access this value.
EXPIRE server:name 120 //after 120s this value will vanish
TTL server:name // obtain after how long it will vanish
RPUSH, LPUSH, LLEN, LRANGE, LPOP, RPOP
SADD, SREM, SISMEMBER, SMEMBERS, SUNION
SMEMBERS returns a list of all members of this set.
HSET, HGETALL, HMSET, HGET, HINCRBY, HDEL like
HMSET user:1001 name "Mary Jones" password "hidden" email "[email protected]"
Adding our MessagesChannel
whenever a client subscribes to MessageChannel, the
#subscribed method will be called, which starts streaming anything you broadcast to the “messages” stream.