Skip to content Skip to sidebar Skip to footer

How To Fire Eventsource Sse Events?

I've recently discovered EventSource, YUI3 has a Gallery module to normalise and fallback behaviour, that's what I've chosen to go with in my example as I use that framework alread

Solution 1:

there are several issues in your approach:

  1. The server-side code that reads the cmd parameter is unreachable because of the infinite loop that sends event data to the client.
  2. You are trying to send an event from the client to the server. It is in the specification name - Server-Sent Events - the server is the sender and the client is the receiver of events. You have options here:
    1. Use the appropriate specification for the job called Web Sockets which is a two-way communication API
    2. Write the logic that makes the desired type of communication possible

If you choose to stay with the SSE API I see two possible scenarios

  1. Reuse the same Event Source connection and store a pool of connections on the server. When the user sends subsequent XMLHttpRequest with the update command, get the EventSource connection from the pool, that was made by this visitor, and send response with it that specifies your custom event type, the default type is message. It is important to avoid entering in the infinite loop that would make another EventSource connection to the client, but the client does not handle it because he made the request with XMLHttpRequest and not with EventSource.
  2. Make all requests with EventSource. Before making a new EventSource request, close the previous one - you can do this from the client or from the server. On the server check the parameters and then send data to client.

Also you can use XMLHttpRequest with (long) polling and thus avoiding the need of using EventSource. Because of the simplicity of your example I can't see a reason to mix the two type of requests.

Post a Comment for "How To Fire Eventsource Sse Events?"