Multiple subscribers with EM-WebSocket or Goliath

691 views Asked by At

So I'm attempting to setup em-websocket (or potentially Goliath), so that users can come to a different route and thusly be subscribed to only that channel, so for example:

example.com/channel_1

Browsers open there will only receive messages published to channel_1, actually to that point it doesn't have to be a route like this I'd settle for using params. So I'm using AMQP and it has the notion of a direct exchange, and routing keys. Is there something analogous to that with websockets?

I've got a Goliath server working but the issue is, that because it uses shared endpoints, I think that all the browsers open with a websocket connection are getting the same messages, here's what I'm doing:

channel.queue(params['channel'], :auto_delete => true).subscribe do |payload|
  config['channel'].push(payload)
end

So this example uses AMQP, which I'd still like to use, but the issue I think lies in that each client is reinstantiating EM::Channel.new, and then the messages are pushed to that channel, I just don't understand how I would have multiple clients subscribed to different channels.

Any help understanding this or guiding me to a more appropriate design pattern.

1

There are 1 answers

0
dj2 On BEST ANSWER

If your channels are pre-defined, you can just do a EM::Channel.new in the config file for your server for each of the channels, then the clients will send/receive messages through the appropriate channel.

If the channels are defined by the users then you'll need to setup a hash (or something) in your config that will store your channels, when a client connects, check the config hash to see if the channel exists, if it doesn't, EM::Channel.new it and continue on. If it exists, use the existing channel.

In the second case you'll need to do some logic on client disconnect to deal with shutting down the channel when all clients are gone or you'll have a memory leak.