Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

13. Networking > Server-Sent Events

Server-Sent Events

In normal HTTP networking with XHR or the <script> tag, the client requests or “pulls” data from the server when it needs it. There is another style of HTTP-based networking that is used by some web applications. In “server push” or “comet,” the client and server establish an HTTP connection, but leave it open indefinitely, which allows the server to push data to the client through that open connection.

It is possible but difficult to implement this style of networking with XHR, but a new HTML5-related standard known as Server-Sent Events defines a simple EventSource API that makes it trivial to receive and respond to messages pushed by the server. To use Server-Sent Events, simply pass a URL to the EventSource() constructor and then listen for message events on the returned object:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial