zhephyn Profile picture
Aug 21 18 tweets 5 min read Read on X
This tutorial is meant to serve as an introduction to Action Cable. For those unfarmiliar with what Action Cable is, basically all things real-time in rails are handled by action cable. I'll take you from the basics of action cable configuration up to the final version of the app
Below is a gif of what we'll build. A simple app where by we navigate to a room in the browser and we see messages appended to that room in real-time without the need for refreshing.
1. Create a new rails app of your desired name. I've chosen action_cable_chat as the name of my app.
2. Navigate to the folder where you've created your new rails app and cd into it
3. Open your rails app in VS code, navigate to the gemfile and uncomment the line related to the Redis gem. In the terminal, run bundle add to install redis as a gem in your rails app.
Redis is the pub-sub system that action cable depends on for real-time message publishing and delivery. This is the major reason as to why we need redis to be installed.
Now that we've installed Redis as a gem, we need to install Redis on our local machine. This is important because we'll need it to start the redis server, another important part of action cable. Download Redis from the official website at redis.io/downloads/
4. Now we'll make some changes to a couple files. First edit your cable.yml to look like below; Image
5. Add this line to your application.rb file
config.action_cable.mount_path = "/cable" Image
6. Add the route for handling the action cable/websocket requests to your routes.rb files by adding the line below,
mount ActionCable.server => "/cable" Image
7. Then edit your development.rb file to look like below,
Add the lines below to your development.rb file
config.action_cable.url = "ws://localhost:3000/cable"
config.action_cable.allowed_request_origins = ["https://localhost:3000",/http:\/\/localhost:*/] Image
8. Create a message model with each message having an attribute of content of type text by running the command below,
=>rails generate model Message content:text
Then migrate the database,
=> rails db:migrate
9. Generate a messages controller to accompany the message model by running the command below,
=>rails g controller Messages index
Edit your messages_controller.rb file to look like below, Image
10. Edit the index.html.erb file to look like below Image
11. Now add the routes for messages in the routes.rb file Image
12. Now generate the channel to handle message broadcasting and appending them to the front-end.
Run the command
=> rails generate channel Chat
This creates a couple files of which 2 are of utmost relevance, chat_channel.rb and chat_channel.js.
13. The chat_channel.js file has 3 functions of which the received function handles broadcasted messages to the front-end/ the view, the connected and disconnected functions log messages to the console when a web socket connection is made and broken respectively. Image
14. The chat_channel.rb is the back-end part of the chat channel, it handles and accepts subscriptions as well sets up streams for the various connected clients. It should look like below; Image
15. The broadcasting happens in the messages controller with the method,
ActionCable.server.broadcast("chat_Best Room", {message: @message.content})
The messages will be received by all consumers listening in on the "chat_Best Room" stream. And that marks the end of the tutorial

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with zhephyn

zhephyn Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(