rexxars / sse-channel

Server-Sent Events "channel" where all messages are broadcasted to all connected clients, history is maintained automatically and server attempts to keep clients alive by sending "keep-alive" packets automatically.
MIT License
111 stars 11 forks source link

Question: How to subscribe client to different channels on demand #19

Open JavoByte opened 7 years ago

JavoByte commented 7 years ago

Hi

I have a problem and I don't exactly know how to solve it. Currently, I'm using this library to subscribe clients to specific events. However, in my case, a client can subscribe to multiple events. So, I have to open an EventSource per event source.

My question is, could it be possible to have only one EventSource and in my node server have a route like events/:eventID/subscribe which, assuming the client has already opened an EventSource and a SseChannel for the eventID exists, add the client to the SseChannel so the next message sent to the channel is received by the client?

The response content type of this url should be application/json just saying something like "Ok, you'll receive updates in your EventSource" and may be, trigger a message in the EventSource.

How would you solve this or attack this problem? I'm a clueless here.

wKovacs64 commented 7 years ago

I ran into this as well and I ended up solving it by constructing my own logical channels rather than utilizing the literal channels included in sse-channel. Each message that gets sent out includes a channels array and the client is responsible for handling channel-targeted messages accordingly (the client knows what channels it cares about). This worked in my environment because I wrote both the API and the client code, and the data is not sensitive (all clients receive every message and that's OK). Whether or not this approach would work for you depends on the details of your specific use case, of course.

elf-pavlik commented 7 years ago

How about having a SseChannel per event and use query params which would have array of events cilents want to subscribe to. Then in the handler of that route you could add that client to all the channels listed in that query param.

joepie91 commented 5 years ago

See #25 for a proposed feature that would solve these requirements.