B
B
BonBon Slick2021-05-04 15:01:04
Fonts
BonBon Slick, 2021-05-04 15:01:04

Websocket or SSE what and when to use, why?

What, when, why and why?
How to choose between the two?

A test case to consider, a chat like Slack / Discord.
Audio + Video, messages.
Chat Groups and their subgroups, rooms.
Chat 1 on 1, or create a conference of 1+ people.

I had to use both, but rather support than writing from scratch, so there was no time to understand why what was chosen was chosen.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav Lyskov, 2018-10-04
@Vlatqa

lucida console?

D
Dmitry Belyaev, 2021-05-04
@BonBonSlick

Websocket is a two way binary protocol.
SSE is a one-way (only server sends data) text protocol.
Both work over HTTP, but Websocket does Switch Protocol (101 response code) while SSE does not.
Websocket - requires additional logic for encoding / decoding frames (messages). In the browser this logic is built in and provided by the WebSocket object , on other platforms it is usually up to the programmer to implement the protocol.
The fact that the protocol is two-way means that either side can send data at any time. The request / response mechanism is not implemented in the protocol, but can be done in the underlying protocols .
The fact that the protocol is binary means that any data can be transmitted through it without restrictions.
The fact that the protocol works on top of HTTP makes it possible to send additional data in URIs or headers when initializing the connection (headers are not available in the browser, but cookies are sent according to common policies)
SSE is essentially a regular HTTP GET request with a long response. This means that no additional logic is required for implementation, in fact it is a regular HTTP.
The client can send its data only when initialized in URIs or headers (the browser implementation again does not give access to header management, but at least it allows you to control the sending of cookies, which by default are sent only to the current domain, and for the case of CORS they can be enabled).
The server can send any number of text messages, each line starts with "data: ", the message ends with a double line feed character, and optionally, the message id can be passed as a separate line before the message.
The fact that the protocol is textual - limits the transmitted data only to text (for example, valid utf8).
SSE is generally cheaper both in terms of resources and transmitted traffic, but has a number of limitations compared to WebSocket.
In terms of browser support for these technologies, everything is about the same, and today we can safely say that support for both technologies is everywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question