tpeczek/Lib.AspNetCore.ServerSentEvents

How to send messages to specific clients?

Closed this issue · 6 comments

how can i sent messages to some clients not all connected clients?

The IServerSentEventsService exposes GetClients() method which will return list of currently connected clients. Every client (represented by IServerSentEventsClient) exposes User property and SendEventAsync method.

if use nginx to load balance, the messages will not sent all clients?
private readonly ConcurrentDictionary<Guid, ServerSentEventsClient> _clients = new ConcurrentDictionary<Guid, ServerSentEventsClient>();

The core aspect of Server Sent Events is persistent connection. In load balancing scenario this connection is being made to a specific instance behind the load balancer and only this instance can send messages to client represented by that connection. If you want to send an update generated in specific instance to clients connected to all instances you need a way for communicating that update across all the instances (one option can be Pub/Sub functionality provided by Redis, but there are others).

Thanks you very mush
Can I send messages into Redis publish channels,then use IServerSentEventsService subscribe the channels,and then use SendEventAsync method send to all connected clients in the load balancing instances?

Yes, that is the general idea.

ok,thanks