Camille Tolsa
Camille Tolsa

Reputation: 261

Websocket-rails cannot broadcast from model

I'm facing a problem using the websocket-rails gem in my rails4 application.
The problem is that I cannot broadcast from my models.

see my model :

class Diffusion < ActiveRecord::Base
  after_create            :ws_update

  def ws_update
    WebsocketRails[:diffusions].trigger 'diffusions.new', {test: self.id}.to_json
  end
end

from another rb file located under lib folder i'm calling this :

Diffusion.create(some_params)

I know for sure that my funtion ws_update is called and I can even see the logs into log/websockets_rails.log that the message has been sent.

There is my coffee/script that should handle the message in my web page

dispatcher = new WebSocketRails("localhost:3000/websocket")
channel = dispatcher.subscribe("diffusions")
channel.bind "diffusions.new", (data) ->
  console.log "GOT DATAAAAAAA"
  return

The "GOT DATAAAA" line is never display can you explain me why ?

Thanks

Upvotes: 3

Views: 272

Answers (1)

phlegx
phlegx

Reputation: 2732

Read here: https://github.com/websocket-rails/websocket-rails/blame/712fd4e35325887956724be388821121a866c7fc/README.md#L177

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

Upvotes: 1

Related Questions