Basic Usage

  • To receive the message in your browser copy and paste this into a script tag.
    javascript
    const evtSource = new EventSource("https://sse.dev/test");
        evtSource.onmessage = function(event) {
          var dataobj = JSON.parse(event.data);
          console.log(dataobj);
        }
  • You will now be able to manipulate the DOM with the incoming data
  • You will receive a message by default every 2 seconds from the server
Test Event Source here

Advanced Usage

Setting the interval

To set the interval pass a "interval" query parameter on the url. eg. for 5 seconds do the following

javascript
const evtSource = new EventSource("https://sse.dev/test?interval=5");
Test Event Source here


Setting the data returned every interval

To set the JSON data returned for each interval, pass a "jsonobj" query parameter on the url. eg.

javascript
const evtSource = new EventSource("https://sse.dev/test?jsonobj={"name":"werner","age":38}");
Test Event Source here


Svelte Demo

See this demo in SvelteJS for a dynamic example of how to use the EventSource API.
Svelte REPL


GraphQL Demo

This returns a basic GraphQL EventSource.
GraphQL Format