Server-Sent Events multiple clients

423 views Asked by At

Maybe I don't understand Server-Sent Events, but I cannot get anything on my specific need. I want to implement Server-Sent Events, with one Asp.net page that sends to multiple clients, depending on the client ID passed via a query string to the server page.

Some of the clients may have the same id, messages must then be broadcasted to the clients depending on their ID.

eg

var source = new EventSource("/test.aspx?l=4298");
source.onmessage = function (event)
{
  document.getElementById("result").innerHTML += event.data + "<br>";
};

On the server I Query the database depending on the ID passed, then if there are records, send some data to notify the client.

I have tried while loops on the server with a 2 second delay, but it is not very reliable, it reports only to some of the clients. Then I tried to run the page without the loop, just by opening the database, but it then opens up a database connection for each client every 3 seconds and that would be too server intense.

What I need is an asp.net page on the server that broadcast to all the connected clients, that would only open the database, eg every 20 seconds and if there are data for a client, it will notify him / them.

If anyone can point me to a book or video or any tutorial I would appreciate it.

0

There are 0 answers