kechol
kechol

Reputation: 1574

How to dispatch message from ActionController in websocket-rails

I'm developing websocket server with Rails 4 and websocket-rails gem.

I'm thinking send a message as http POST message (because it's convenient to customize headers or so).

I'd like to dispatch websocket message from my controller, but I may not use WebsocketRails::BaseController.send_message. How can I do this or something wrong?

here's code:

# message_controller.rb

# This controller doesn't inherit from WebsocketRails::BaseController
class MessageController < ActionController::Base
  def create
    # do something with params and models
    Message.create params

    # then, I want to dispatch message here!
  end
end

Upvotes: 2

Views: 875

Answers (1)

kechol
kechol

Reputation: 1574

I found the answer by myself, just in the readme.

https://github.com/websocket-rails/websocket-rails

Broadcast to the channel from anywhere inside your Rails application. An existing controller, a model, a background job, or a new WebsocketRails controller.

latest_post = Post.latest
WebsocketRails[:posts].trigger 'new', latest_post

Upvotes: 4

Related Questions