WebSockets vs. Server-Sent events/EventSource

Questions : WebSockets vs. Server-Sent events/EventSource

Both WebSockets and Server-Sent Events are capable of pushing data to browsers. To me they seem to be competing technologies. What is the difference between them? When would you choose one over the other?

Total Answers: 6 Answers 6


Popular Answers:

  1. Websockets and SSE (Server Sent Events) are both capable of pushing data to browsers, however they are not competing technologies.

    Websockets connections can both send data to the browser and receive data from the browser. A good example of an application that could use websockets is a chat application.

    SSE connections can only push data to the browser. Online stock quotes, or twitters updating timeline or feed are good examples of an application that could benefit from SSE.

    In practice since everything that can be done with SSE can also be done with Websockets, Websockets is getting a lot more attention and love, and many more browsers support Websockets than SSE.

    However, it can be overkill for some types of application, and the backend could be easier to implement with a protocol such as SSE.

    Furthermore SSE can be polyfilled into older browsers that do not support it natively using just JavaScript. Some implementations of SSE polyfills can be found on the Modernizr github page.

    Gotchas:

    • SSE suffers from a limitation to the maximum number of open connections, which can be specially painful when opening various tabs as the limit is per browser and set to a very low number (6). The issue has been marked as “Won’t fix” in Chrome and Firefox. This limit is per browser + domain, so that means that you can open 6 SSE connections across all of the tabs to www.example1.com and another 6 SSE connections to www.example2.com (thanks Phate).
    • Only WS can transmit both binary data and UTF-8, SSE is limited to UTF-8. (Thanks to Chado Nihi).
    • Some enterprise firewalls with packet inspection have trouble dealing with WebSockets (Sophos XG Firewall, WatchGuard, McAfee Web Gateway).

    HTML5Rocks has some good information on SSE. From that page:

    Server-Sent Events vs. WebSockets

    Why would you choose Server-Sent Events over WebSockets? Good question.

    One reason SSEs have been kept in the shadow is because later APIs like WebSockets provide a richer protocol to perform bi-directional, full-duplex communication. Having a two-way channel is more attractive for things like games, messaging apps, and for cases where you need near real-time updates in both directions. However, in some scenarios data doesn’t need to be sent from the client. You simply need updates from some server action. A few examples would be friends’ status updates, stock tickers, news feeds, or other automated data push mechanisms (e.g. updating a client-side Web SQL Database or IndexedDB object store). If you’ll need to send data to a server, XMLHttpRequest is always a friend.

    SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol. In addition, Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events.


    TLDR summary:

    Advantages of SSE over Websockets:

    • Transported over simple HTTP instead of a custom protocol
    • Can be poly-filled with javascript to “backport” SSE to browsers that do not support it yet.
    • Built in support for re-connection and event-id
    • Simpler protocol
    • No trouble with corporate firewalls doing packet inspection

    Advantages of Websockets over SSE:

    • Real time, two directional communication.
    • Native support in more browsers

    Ideal use cases of SSE:

    • Stock ticker streaming
    • twitter feed updating
    • Notifications to browser

    SSE gotchas:

    • No binary support
    • Maximum open connections limit

  2. Websocket VS SSE


    Web Sockets – It is a protocol which provides a full-duplex communication channel over a single TCP connection. For instance a two-way communication between the Server and Browser Since the protocol is more complicated, the server and the browser has to rely on library of websocket which is socket.io

    Example - Online chat application. 

    SSE(Server-Sent Event) – In case of server sent event the communication is carried out from server to browser only and browser cannot send any data to the server. This kind of communication is mainly used when the need is only to show the updated data, then the server sends the message whenever the data gets updated. For instance a one-way communication between the Server to Browser. This protocol is less complicated, so no need to rely on the external library JAVASCRIPT itself provides the EventSource interface to receive the server sent messages.

    Example - Online stock quotes or cricket score website. 
  3. One thing to note:
    I have had issues with websockets and corporate firewalls. (Using HTTPS helps but not always.)

    See https://github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software https://github.com/sockjs/sockjs-client/issues/94

    I assume there aren’t as many issues with Server-Sent Events. But I don’t know.

    That said, WebSockets are tons of fun. I have a little web game that uses websockets (via Socket.IO) (http://minibman.com)

  4. they are different in semantics.

    websocket has a native semantic meaning of “bidirectional data stream”.

    while sse has a native semantic meaning of “publish-subscribe pattern” or “request-respond pattern, despite the response is a stream”.

    of course you can implement a layer of “pub-sub pattern” or “req-res pattern” over websocket by yourself.

  5. for the gitlab RSAAuthentication yes

    Host gitlab.com RSAAuthentication yes IdentityFile ~/.ssh/your_private_key_name IdentitiesOnly yes 

    doc is here

  6. If SSH port number is not 22(default), add Port xx in ~/.ssh/config

    In my case (synology),

    Host my_synology Hostname xxxx.synology.me IdentityFile ~/.ssh/id_rsa_xxxx User myname Port xx 

    Then clone using Host title in config. (“my_synology”. to avoid @chopstik ‘s “*”)

    git clone my_synology:path/to/repo.git 
  7. If you’re like me, you can:

    • Keep your ssh keys organized

    • Keep your git clone commands simple

    • Handle any number of keys for any number of repositories.

    • Reduce your ssh key maintenance.

    I keep my keys in my ~/.ssh/keys directory.

    I prefer convention over configuration.

    I think code is law; the simpler it is, the better.

    STEP 1 – Create Alias

    Add this alias to your shell: alias git-clone='GIT_SSH=ssh_wrapper git clone'

    STEP 2 – Create Script

    Add this ssh_wrapper script to your PATH:

    #!/bin/bash # Filename: ssh_wrapper if [ -z ${SSH_KEY} ]; then SSH_KEY='github.com/l3x' # <= Default key fi SSH_KEY="~/.ssh/keys/${SSH_KEY}/id_rsa" ssh -i "${SSH_KEY}" "[email protected]" 

    EXAMPLES

    Use github.com/l3x key:

    KEY=github.com/l3x git-clone https://github.com/l3x/learn-fp-go 

    The following example also uses the github.com/l3x key (by default):

    git-clone https://github.com/l3x/learn-fp-go 

    Use bitbucket.org/lsheehan key:

    KEY=bitbucket.org/lsheehan git-clone [email protected]:dave_andersen/exchange.git 

    NOTES

    Change the default SSH_KEY in the ssh_wrapper script to what you use most of the time. That way, you don’t need to use the KEY variable most of the time.

    You may think, “Hey! That’s a lot going on with an alias, a script and some directory of keys,” but for me it’s convention. Nearly all my workstations (and servers for that matter) are configured similarly.

    My goal here is to simplify the commands that I execute regularly.

    My conventions, e.g., Bash scripts, aliases, etc., create a consistent environment and helps me keep things simple.

    KISS and names matter.

    For more design tips check out Chapter 4 SOLID Design in Go from my book: https://www.amazon.com/Learning-Functional-Programming-Lex-Sheehan-ebook/dp/B0725B8MYW

    Hope that helps. – Lex

  8. Here’s the ssh key hack i found while finding solution to this problem:

    For example you have 2 different set of keys:

    key1, key1.pub, key2, key2.pub

    Keep these keys in your .ssh directory

    Now in your .bashrc or .bash_profile alias file, add these commands

    alias key1='cp ~/.ssh/key1 id_rsa && cp ~/.ssh/key1.pub id_rsa.pub'

    alias key2='cp ~/.ssh/key2 id_rsa && cp ~/.ssh/key2.pub id_rsa.pub'

    Voila! You have a shortcut to switch keys whenever you want!

    Hope this works for you.

Tasg: html, browser