em-websocket send() send from one client to another through 2 servers

211 Views Asked by At

I have two websocket clients, and I want to exchange information between them.

Let's say I have two instances of socket servers, and 1st is retrieve private information, filter it and send to the second one.

require 'em-websocket'

EM.run do
  EM::WebSocket.run(host: '0.0.0.0', port: 19108) do |manager_emulator|
    # retrieve information. After that I need to send it to another port (9108)
  end

  EM::WebSocket.run(host: '0.0.0.0', port: 9108) do |fake_manager|
    # I need to send filtered information here
  end
end

I've tried to do something, but I got usual dark code and I don't know how to implement this functionality.

2

There are 2 best solutions below

1
On BEST ANSWER

I've found a way how to do it through em-websocket gem! You need just define variables outside of eventmachine block. Something like that

require 'em-websocket'

message_sender = nil

EM.run do
  # message sender
  EM::WebSocket.run(host: '0.0.0.0', port: 19108) do |ws|
    ws.onopen { message_sender = ws }
    ws.onclose { message_sender = nil }
  end

  # message receiver
  EM::WebSocket.run(host: '0.0.0.0', port: 9108) do |ws|
    ws.onmessage { |msg| message_sender.send(msg) if message_sender }
  end
end
1
On

I'm not sure how you would do that using EM.

I'm assuming you will need to have the fake_manager listen to an event triggered by the manager_emulator.

It would be quite easy if you'd be using a websocket web-app framework. For instance, on the Plezi web-app framework you could write something like this:

# try the example from your terminal.
# use http://www.websocket.org/echo.html in two different browsers to observe:
#
# Window 1: http://localhost:3000/manager
# Window 2: http://localhost:3000/fake

require 'plezi'

class Manager_Controller
    def on_message data
        FakeManager_Controller.broadcast :_send, "Hi, fake! Please do something with: #{data}\r\n- from Manager."
        true
    end
    def _send message
        response << message
    end
end
class FakeManager_Controller
    def on_message data
        Manager_Controller.broadcast :_send, "Hi, manager! This is yours: #{data}\r\n- from Fake."
        true
    end
    def _send message
        response << message
    end
end
class HomeController
    def index
        "use http://www.websocket.org/echo.html in two different browsers to observe this demo in action:\r\n" +
        "Window 1: http://localhost:3000/manager\r\nWindow 2: http://localhost:3000/fake\r\n"
    end
end

# # optional Redis URL: automatic broadcasting across processes or machines:
# ENV['PL_REDIS_URL'] = "redis://username:[email protected]:6379"

# starts listening with default settings, on port 3000
listen

# Setup routes:
# They are automatically converted to the RESTful route: '/path/(:id)'
route '/manager', Manager_Controller
route '/fake', FakeManager_Controller
route '/', HomeController

# exit terminal to start server
exit

Good Luck!

P.S.

If you're going to keep to EM, you might consider using Redis to push and subscribe to events between the two ports.