EventMachine and em-websocket - reading from the queue and clicking on the channel

I use eventmachine to read from a HornetQ topic, click on the feed that EM web connection connections are subscribed to. I need to prevent the @ topic.receive loop from blocking from locking, so I created proc and I call EventMachine.defer without a callback. It will work forever. It works great. I could also use Thread.new.

My question is, is this the right way to read from a stream / queue and transfer data to a channel, and is there a better / any other way to do this?

require 'em-websocket' require 'torquebox-messaging' class WebsocketServer def initialize @channel = EM::Channel.new @topic = TorqueBox::Messaging::Topic.new('/topics/mytopic') end def start EventMachine.run do topic_to_channel = proc do while true msg = @topic.receive @channel.push msg end end EventMachine.defer(topic_to_channel) EventMachine::WebSocket.start(:host => "127.0.0.1", :port => 8081, :debug => false) do |connection| connection.onopen do sid = @channel.subscribe { |msg| connection.send msg } connection.onclose do @channel.unsubscribe(sid) end end end end end end WebsocketServer.new.start 
+4
source share
1 answer

This is normal, but 20 threads will appear in EM.defer, so I would avoid this in your use case. In general, I would completely avoid EM, especially the Java reactor, since we never finished it.

Torquebox has a built-in function stomp over websockets, which would be much better in this context and solves the problem with other encapsulation problems.

If you really want to stick with EM for this, then I would use Thread.new instead of deferring so that there are no 19 idle threads occupying the additional ram for no reason.

+1
source

Source: https://habr.com/ru/post/1443265/


All Articles