I have the following code to setup my websocket implementaion using ActionCable in a Rails API project
routes.rb
mount ActionCable.server => '/cable'
application.rb
config.action_cable.mount_path = nil
development.rb
config.action_cable.url = "ws://localhost:3001/cable"
config.action_cable.disable_request_forgery_protection = true
channels/application_cable/channel.rb
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
channels/jobs_channel.rb
class JobsChannel < ApplicationCable::Channel
def subscribed
stream_from "jobs_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
using redis. i have my cable.yml
development:
adapter: redis
url: "redis://localhost:6379/0"
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: <your_app_name>_production
and because i want to have both my rails server and my websocket server running at the same time, i setup a file cable/config.ru
require ::File.expand_path('../../config/environment', __FILE__)
Rails.application.eager_load!
run ActionCable.server
Now i start my rails server as rails s and start my websocket server as bundle exec puma -p 3001 cable/config.ru. I also make sure my redis server is running.
On postman, i setup like this and it is able to connect to the websocket server

To be able to send data to the websocket, i should be able to do something like this
serialized message = {"data": "Hello world"}
ActionCable.server.broadcast("jobs_channel", serialized_message)
But it seems not to be working. I cannot seem to be able to pass data via the websocket. What am i doing wrong?
Would appreciate if anyone can point me in the right direction.
Thanks