Ruby On Rails WebSockets

Learn how to create real-time features with Ruby on Rails websockets and an example.

Ruby on Rails is a popular web framework that has built-in support for WebSocket connections. WebSockets allow for real-time communication between a client and server, and they are becoming increasingly popular in web applications. This tutorial will show you how to use WebSockets in Ruby on Rails with Action Cable.

Setting Up the Rails App

To get started, you will need to create a new Rails app. You can do this by running the following command in your terminal:

rails new my_app

Once the app is created, you will need to add the Action Cable gem to your Gemfile. This gem adds support for WebSockets to your Rails app. You can add the gem with the following command:

gem 'actioncable', '~> 5.2'

Once the gem is installed, you will need to run the following command to create the necessary files for Action Cable:

rails g action_cable:install

This command will create two files: one called action_cable.rb and one called channels.rb. The action_cable.rb file is used to configure Action Cable, while the channels.rb file is used to create the different channels that your app will use.

Creating a Channel

Once the files have been created, you can create a new channel in the channels.rb file. For example, you can create a channel for chat messages with the following code:

class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from "chat_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

This code creates a new channel called ChatChannel. The subscribed method is called when a user subscribes to the channel, and the stream_from method is used to stream data from the chat_channel. The unsubscribed method is called when the user unsubscribes from the channel.

Sending Messages

Once the channel has been created, you will need to create a controller to handle incoming messages. For example, you can create a MessagesController with the following code:

class MessagesController < ApplicationController
  def create
    ActionCable.server.broadcast "chat_channel",
                                 message: params[:message]
  end
end

This controller uses the ActionCable.server.broadcast method to send the message to the chat_channel. The message is passed in as a parameter to the broadcast method.

Receiving Messages

In order to receive messages, you will need to create a JavaScript file to handle the incoming messages. You can create a file called chat.js with the following code:

App.cable.subscriptions.create('ChatChannel', {  
  received: function(data) {
    $('#messages').append(data.message);
  }
});

This code creates a subscription to the ChatChannel and calls the received function when a new message is received. The received function appends the message to the #messages element on the page.

Conclusion

In this tutorial, you learned how to use WebSockets in Ruby on Rails with Action Cable. You saw how to create a channel, send messages, and receive messages. You also saw how to use JavaScript to handle incoming messages. With this knowledge, you can now create real-time applications with Ruby on Rails and WebSockets.

Answers (0)