Learning ActionCable

2024-06-12 rails postgresql

How to generate a simple ActionCable on rails app. If you built the app using postgresql, it can be used as the adapter(no need to install Redis and so on).

rails new myapp --database=postgresql
cd myapp
rails g channel notification
rails g scaffold room name occupied:boolean

You may need to update config/database.yml before creating database.

  username: username
  password: yourpassword

Initialize database.

rails db:create db:migrate

Modify the following files along with your purpose.

config/cable.yml

adapter: postgresql

app/channels/notification_channel.rb

stream_from "notification_channel"

app/controllers/rooms_controller.rb

# Add this where you want to notify the changes.
ActionCable.server.broadcast "notification_channel", {content: @room}

app/javascript/channels/notification_channel.js

// Add the process when receiving messages like:
location.reload();

Next step: How to integrate this into minimal rails project..?