Прост в реализации и использовании, как на стороне клиента, так и на стороне сервера, более производителен для сервера, меньше задержка ответа клиента vs Long Polling
built-in support for reestablishing dropped connections, as well as recovery of messages the client may have missed while disconnected.
By default, if the connection is dropped, then the browser will automatically reestablish the connection. The SSE specification recommends a 2–3 second delay, which is a common default for most browsers, but the server can also set a custom interval at any point by sending a retry command to the client.
message – получено сообщение, доступно как event.data.
open – соединение открыто.
error – не удалось установить соединение, например, сервер вернул статус 500
lastEventId - ИД события
retry - мс на переподключение при обрыве связи
Patterns
Адресация сообщений возможна по event
пример: ИДПользователя_ДоговорСтатусИзменен, где клиент подписывается на события для конкретного пользователя, сервер ИД пользователя получает при аутентификации.
Обработка потери соединения
Setting an ID lets the browser keep track of the last event fired so that if, the connection to the server is dropped, a special HTTP header (Last-Event-ID) is set with the new request. This lets the browser determine which event is appropriate to fire. The message event contains a e.lastEventId property.
server can implement several different strategies:
If lost messages are acceptable, then no event IDs or special logic is required: simply let the client reconnect and resume the stream.
If message recovery is required, then the server needs to specify IDs for relevant events, such that the client can report the last seen ID when reconnecting.
Also, the server needs to implement some form of a local cache to recover and retransmit missed messages to the client.
. - варианты реализации на сервере получить сообщения после last-event-id: Redis Streams