Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:28:06+00:00 2026-06-09T20:28:06+00:00

So I understand the concept of server-sent events ( EventSource ): A client connects

  • 0

So I understand the concept of server-sent events (EventSource):

  • A client connects to an endpoint via EventSource
  • Client just listens to messages sent from the endpoint

The thing I’m confused about is how it works on the server. I’ve had a look at different examples, but the one that comes to mind is Mozilla’s: http://hacks.mozilla.org/2011/06/a-wall-powered-by-eventsource-and-server-sent-events/

Now this may be just a bad example, but it kinda makes sense how the server side would work, as I understand it:

  • Something changes in a datastore, such as a database
  • A server-side script polls the datastore every Nth second
  • If the polling script notices a change, a server-sent event is fired to the clients

Does that make sense? Is that really how it works from a barebones perspective?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:28:07+00:00Added an answer on June 9, 2026 at 8:28 pm

    The HTML5 doctor site has a great write-up on server-sent events, but I’ll try to provide a (reasonably) short summary here as well.

    Server-sent events are, at its core, a long running http connection, a special mime type (text/event-stream) and a user agent that provides the EventSource API. Together, these make the foundation of a unidirectional connection between a server and a client, where messages can be sent from server to client.

    On the server side, it’s rather simple. All you really need to do is set the following http headers:

    Content-Type: text/event-stream
    Cache-Control: no-cache
    Connection: keep-alive
    

    Be sure to respond with the code 200 and not 204 or any other code, as this will cause compliant user agents to disconnect. Also, make sure to not end the connection on the server side. You are now free to start pushing messages down that connection. In nodejs (using express), this might look something like the following:

    app.get("/my-stream", function(req, res) {
        res.status(200)
           .set({ "content-type"  : "text/event-stream"
                , "cache-control" : "no-cache"
                , "connection"    : "keep-alive"
                })
    
        res.write("data: Hello, world!\n\n")
    })
    

    On the client, you just use the EventSource API, as you noted:

    var source = new EventSource("/my-stream")
    source.addEventListener("message", function(message) {
        console.log(message.data)
    })
    

    And that’s it, basically.

    Now, in practice, what actually happens here is that the connection is kept alive by the server and the client by means of a mutual contract. The server will keep the connection alive for as long as it sees fit. Should it want to, it may terminate the connection and respond with a 204 No Content next time the client tries to connect. This will cause the client to stop trying to reconnect. I’m not sure if there’s a way to end the connection in a way that the client is told not to reconnect at all, thereby skipping the client trying to reconnect once.

    As mentioned client will keep the connection alive as well, and try to reconnect if it is dropped. The algorithm to reconnect is specified in the spec, and is fairly straight forward.

    One super important bit that I’ve so far barely touched on however is the mime type. The mime type defines the format of the message coming down the connecting. Note however that it doesn’t dictate the format of the contents of the messages, just the structure of the messages themselves. The mime type is extremely straight forward. Messages are essentially key/value pairs of information. The key must be one of a predefined set:

    • id – the id of the message
    • data – the actual data
    • event – the event type
    • retry – milleseconds the user agent should wait before retrying a failed connection

    Any other keys should be ignored. Messages are then delimited by the use of two newline characters: \n\n

    The following is a valid message: (last new line characters added for verbosity)

    data: Hello, world!
    \n
    

    The client will see this as: Hello, world!.

    As is this:

    data: Hello,
    data: world!
    \n
    

    The client will see this as: Hello,\nworld!.

    That pretty much sums up what server-sent events are: a long running non-cached http connection, a mime type and a simple javascript API.

    For more information, I strongly suggest reading the specification. It’s small and describes things very well (although the requirements of the server side could possibly be summarized a bit better.) I highly suggest reading it for the expected behavior with certain http status codes, for instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A concept check: from what I understand, the order of columns in a primary
I have a question to understand the concept of ASP.NET with each client browser.
i have a FileServer and a FileClient, server sends a file, when client connects.
I understand the concept and reasons behind using the using statement, and I use
I am trying to understand the concept of currying and calling a function which
I am trying to understand the concept of timestamps in request headers in web
I'm trying to better understand the concept of 'autocommit' when working with a Postgres
What's the concept behind zip compression? I can understand the concept of removing empty
I'm a beginner in C and I'm trying to understand the concept of pointer
I understand the IOC concept, which we can mix-and-match different classes using wiring. Every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.