Mastering Real-Time Data Exchange with WebSocket and SSN

Aryan Bisht
6 min readSep 24, 2023

--

Title Image

In the era of internet everything need to quick and real-time. And as all the data are stored in server we need some way to retrieve it. There are time when we need data only when user asked for it or click some button or on any particular event. But sometimes there is a need to deliver data and have it go to/from users instantly.

When we want data on once or twice from app we can use simple HTTP request as it is simple to integrate in our app. But in case of instant update or real-time data we will be using some other way to achieve this.

So there are two ways to achieve real-time update think :

  1. SSN (Server Sent Event)
  2. WebSocket
  3. Client Polling

I will be explaining each of these 3 techinques and in next few article I will be teaching how to implement these in flutter (dart). Because before implementing any thing we should know what’s and how it’s doing.

1. Client Polling

The client sends requests to the server at regular intervals for new updates. Although this technique is not used much nowadays, it can be preferred for some small-medium size projects. It is easy to implement. This technique does not provide a fully-real time system that depends on the request intervals.

In the polling technique, requests are sent and managed by the client side. Requests are sent by the client even if there is no update on the server and making multiple network request doesn’t just cut it because we need an active connection to the server at all times and soon will overflow the server from the requests.

2. SSN (Server-Sent Event)

Server-Sent Events (SSE) is a technology that enables a web server to push real-time updates to a web browser over a single, long-lived HTTP connection. SSE is often used to implement server-to-browser communication in web applications when real-time data updates are required. SSE is a technology that provides asynchronous communication with event stream from server to the client over HTTP for web applications. The server can send un-directional messages/events to the client and can update the client asynchronously. Almost every browser is supporting the SSE except Internet Explorer :).

Server-sent event (SSE) enables servers to send messages from the server to the client without any polling or long-polling

Here are some key points about Server-Sent Events:

  1. One-way Communication: SSE allows the server to send data to the client (browser) without the client needing to request it repeatedly. This is in contrast to traditional polling or WebSocket-based solutions.
  2. Event Stream: Data is sent as a continuous stream of events in a specific text-based format. Each event consists of a type and data payload.
  3. Simple API: SSE is relatively easy to implement on both the server and the client side. On the client side, JavaScript provides an EventSource API for working with SSE.
  4. Connection Resilience: SSE connections are designed to be resilient. If the connection is interrupted, the client will automatically attempt to reconnect, reducing the need for complex error-handling logic.
  5. Text-Based: SSE uses a text-based format, making it suitable for sending structured data like JSON. However, it’s not suitable for sending binary data.
  6. Use Cases: SSE is commonly used in applications that require real-time updates, such as live news feeds, stock market updates, social media notifications, and online gaming.
  7. Compatibility: SSE is supported by most modern web browsers, including Chrome, Firefox, Safari, Edge, and Opera. However, Internet Explorer does not support it, so alternative approaches may be needed for IE compatibility.
  8. Disadvantage: One potential downside of using Server-Sent Events is the limitations in data format. Since SSE is restricted to transporting UTF-8 messages, binary data is not supported.

In summary, Server-Sent Events is a straightforward and efficient way to establish a unidirectional real-time communication channel from the server to the client in web applications, making it a valuable tool for building interactive and dynamic web experiences.

3. WebSocket

Websocket is a very popular technology that provides bi-directional data transfer for client and server communication on real-time applications. Websocket is not based on HTTP protocol, so it requires additional installation and integrations to use it. It is difficult to implement compared to other technologies that were mentioned above. WebSocket is a protocol, similar to HTTP, but with a crucial difference — it is bidirectional, meaning it keeps the connection between the client and server open. This allows us to push changes to the client whenever necessary, as opposed to using HTTPS. WebSockets are particularly useful in situations where immediate communication with clients is necessary.

  1. Bidirectional: WebSockets provide full-duplex, bidirectional communication. Both the client and server can send messages to each other at any time.
  2. WebSocket Protocol: WebSockets use their own WebSocket protocol, which is different from HTTP. WebSocket connections are typically long-lived and allow for efficient, low-latency communication.
  3. Low Overhead: WebSockets have lower overhead compared to SSE because they don’t rely on the request-response model of HTTP. This makes them suitable for applications requiring high interactivity and low latency, such as chat applications and online gaming.
  4. Complexity: Implementing WebSockets can be more complex than SSE due to the bidirectional nature of the communication. You need to handle both sending and receiving messages on both sides.
  5. Browser Support: WebSockets are also well-supported by modern browsers and can be used in various platforms, including web, mobile apps, and desktop applications.
  6. Disadvantage: When a WebSocket connection is closed (e.g. due to network issues), the client does not try to reconnect to the server, which means you’ll need to write extra code to poll the server, re-establishing the connection when it is available again

When to Use SSE:

  • Use SSE when you primarily need to push server updates to the client, and the client doesn’t need to send frequent messages back to the server.
  • SSE is suitable for applications like real-time news feeds, stock market updates, or monitoring systems where the server sends periodic updates to clients.

When to Use WebSockets:

  • Use WebSockets when you need bidirectional communication between the client and server. This is useful for chat applications, online gaming, collaborative tools, and any scenario where both sides need to send messages.
  • WebSockets are ideal for applications that require low-latency, real-time interactions.

In summary, the choice between SSE and WebSockets depends on the specific requirements of your application. SSE is simpler and more suitable for unidirectional updates, while WebSockets provide bidirectional communication with lower latency, making them suitable for more interactive applications.

One important point I want to mention is that to implement any of these technique we have to make make changes on both side back-end and front-end.

In the next few article I will be explaining how to implement these method in flutter. I will those link the article below here after they are published. Make sure to read them too. [that’s why you are here :-) ]

More from Aryan Bisht

You can buy me coffee at :- https://www.buymeacoffee.com/aryanbisht

Level Up Coding And Development Journey

Thanks for being a part of our community! Before you go:

  • 👏 If you find this helpful Clap for the story and follow the author

(Your support is crucial for me to produce more articles like these, especially since monetization options are available in my country.)

--

--