rails5 ActionCable in chatroom, 为之后开发狼人多人游戏做准备

• 整个流程
After rails new chat and bundle install, we add some related in Gemfile, so now it will look like this:

[1] 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:

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

where :session is the name for this form, when in use in SessionsController, we obtain the information in this form by

And url: sessions_path is for directing the action to SessionsController#create.

## In MessagesController.rb

we use index to render the view

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

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.

## Redis

reds is a key-value store, often referred to as a NoSQL database. Key features are that: [1] the data can be retrieved by key later in use. [2] the key-value will be stored permanently.

Compared with

we choose to use INCR because it is a atomic operation which will work as expected if multiple users want to access this value.

### List operation

RPUSH, LPUSH, LLEN, LRANGE, LPOP, RPOP

### Set operation

SADD, SREM, SISMEMBER, SMEMBERS, SUNION
where SMEMBERS returns a list of all members of this set.

### Sorted Set

ZADD, ZRANGE

### Hashes

HSET, HGETALL, HMSET, HGET, HINCRBY, HDEL like

whenever a client subscribes to MessageChannel, the #subscribed method will be called, which starts streaming anything you broadcast to the “messages” stream.