Search code examples
ruby-on-railsrubyfaye

Ruby on Rails: faye-websocket-ruby, undefined method 'on'


I'm working to set up a server-side websocket on my Rails application and am unable to continue because I am receiving this error message

"undefined method 'on' for #< Faye::WebSocket:0x00000007bfb808 >".

I'm using code from the actual README of faye-websocket-ruby's github and the on method is not working.

This is my simple test. It receives the request and creates a Faye::WebSocket object successfully. But the action then crashes because it can't use the 'on' method.

if Faye::WebSocket.websocket?(env)
  ws = Faye::WebSocket.new(env)
  logger.debug "WE HAVE A WEBSOCKET!"

  ws.on :message do |event|
    logger.debug "WE HAVE A MESSAGE!"
  end
end

It is part of the API and used in numerous examples. Does anyone know why this could be happening?


Solution

  • The README is outdated. I looked at ws.methods and couldn't find the on method, so I started looking through the source code. It looks like it was removed, or at least moved to websocket-driver, which would have introduced its own problems trying to integrate it. You can find the new correct syntax in one of the examples though.

    I replaced ws.on :message do |event| with ws.onmessage = lambda do |event| and no more crashing.