2

I'm trying to broadcast from my controller a message to all registered clients. The better way I found to do this is to create a Faye client in my controller, send my message, and close it right after message is sent.

#my_controller.rb

EM.run {
          ws = Faye::WebSocket::Client.new(Rails.application.config.websocket_url)
          ws.send(JSON.dump(this_order.service_json))
          EM.stop
      }

Although this code partially works, it closes all my browser connections to Faye.

Faye is implemented in a middleware, like this:

class FayeMiddleware
    KEEPALIVE_TIME = 30 # in seconds

    def initialize(app)
      @app = app
      @clients = []
    end

    def call(env)
        if Faye::WebSocket.websocket?(env)
            # WebSockets logic goes here
            ws = Faye::WebSocket.new(env, nil, {ping: KEEPALIVE_TIME })
            ws.on :open do |event|
              p [:open, ws.object_id]
              @clients << ws
            end

            ws.on :close do |event|
              p [:close, ws.object_id, event.code, event.reason]
              @clients.delete(ws)
              ws = nil
            end

            ws.on :message do |event|
              p [:message, event.data]
              puts event.data
              @clients.each {|client| client.send(event.data) }
            end
            # Return async Rack response
            ws.rack_response
        else
            @app.call(env)
        end
    end
end

I made this code referring to this tutorial.

I don't understand why all my web sockets get closed when I stop my EM. Can anyone help me ?

Shrolox
  • 663
  • 6
  • 22
  • Make a module and use it this way . . http://stackoverflow.com/questions/33567167/how-can-i-push-to-faye-server-from-rails-controller/33567635#33567635 – Anil Yadav Apr 12 '16 at 16:02

3 Answers3

1

I found a solution:

EM.run {
          ws = Faye::WebSocket::Client.new(Rails.application.config.websocket_url)
          ws.on :open do |event|
            ws.send(JSON.dump(this_order.service_json))
            ws.close
          end
      }

This waits for the socket to open, then send the message and closes. No need to stop the EventMachine i guess.

Shrolox
  • 663
  • 6
  • 22
1

Try this may be it will help you ..

How can I push to Faye Server from Rails Controller?

Community
  • 1
  • 1
Anil Yadav
  • 1,086
  • 7
  • 18
-2

Using the WebSocket client

The client supports both the plain-text ws protocol and the encrypted wss protocol, and has exactly the same interface as a socket you would use in a web browser. On the wire it identifies itself as hybi-13.

require 'faye/websocket'
require 'eventmachine'

EM.run {
  ws = Faye::WebSocket::Client.new('ws://www.example.com/')

  ws.on :open do |event|
  p [:open]
  ws.send('Hello, world!')
end

ws.on :message do |event|
  p [:message, event.data]
end

ws.on :close do |event|
  p [:close, event.code, event.reason]
  ws = nil
end
}

The WebSocket client also lets you inspect the status and headers of the handshake response via its status and headers methods.

To connect via a proxy, set the proxy option to the HTTP origin of the proxy, including any authorization information and custom headers you require:

ws = Faye::WebSocket::Client.new('ws://www.example.com/', [], {:proxy => {
         :origin  => 'http://username:password@proxy.example.com',
         :headers => {'User-Agent' => 'ruby'}
        }
    })

for more help use this link

Rehan Munir
  • 245
  • 1
  • 3