Open martinbarilik opened 4 years ago
By setting up threads yourself, you're working against how EM is supposed to be used. It's a single-threaded reactor, just like JavaScript in the browser, and all I/O is asynchronous. So, if you want to start sockets for a set of users you should do something like this:
EM.run {
users.each do |user|
ws_logger.info "User##{user.id} started initialization"
ws = Faye::WebSocket::Client.new('wss://www.bitmex.com/realtime')
ws.on :open do
# ... some code
ws_logger.info "Socket started in ##{Thread.current.object_id}"
end
ws.on :message # ...
ws.on :close # ...
end
}
Another problem we are facing is to create multiple websocket connections for separate users.
Every user has its own settings and has to be able to connect to its own websocket server with client.
What is the problem is the fact user's first thread is merged into second.
So far so good, output:
And foo.thread_id of user#1 is 76800. But if another user runs the method:
and user#2 foo.thread_id is 74500. Now both are running in the same thread and if terminate #76800, ws is closed for both users. What is the right way to do it? I even tried EM.defer, but it does the same.
Also, we experience this: https://github.com/faye/faye-websocket-ruby/issues/89 with passenger and nginx + postgresql.