15 Haziran 2021 Salı

Server Sent Events Nedir

Giriş
Açıklaması şöyle. Server Sent Events (SSE) Real Time Notification (Gerçek Zamanlı Bildirimler) içindir. SSE tüm tarayıcılar tarafından desteklenir.
Server Sent Events (SSEs) is a server push technology enabling a client to receive automatic updates from a server via HTTP connection.
Mesaj Formatı Nasıldır?
Açıklaması şöyle
The events are just a stream of UTF-8 encoded text data in a format defined by the Specification. The important aspect here is that the format defines the fields that the SSE message should have, but it does not mandate a specific type for the payload, leaving the freedom of choice to the users.
Bir başka açıklama şöyle
How does SSE work ?
A client subscribes to a stream from a server and the server will send messages (“event-stream”) to the client until the server or the client closes the stream. It is up to the server to decide when and what to send the client.

The event-stream is a simple stream of text data which must be encoded using UTF-8. Messages in the event-stream are separated by a pair of newline characters (“\n”).

The following field names are defined in the specification:

Event: The event’s type. It will allow you to use the same stream for different content. A client can decide to “listen” only to one type of event or to interpret differently each event type.

Data: The data field for the message. You can put consecutive “data” lines.

ID: ID for each event-stream. Useful to track lost messages.

Retry: The time to use before the browser attempts a new connection after all connections are lost (in milliseconds).
Id ve Event seçenekleri isteğe bağlıdır. Her satır "\n" karakteri ile biter. Mesaj sonunda da "\n" karakteri vardır.
Örnek 
Eğer metin olarak bakarsa şöyledir
"event: " sometext\n
"data: " sometext\n
\n
Örnek
{
 "id": "message id <optional>",
 "event": "event type <optional>",
 "data": "event data –plain text, JSON, XML… <mandatory>"
}
SSE İçin Context-Type Nasıldır?
Açıklaması şöyle
For the SSE to work, the server needs to tell the client that the response’s content-type is text/eventstream
Örnek
İstemci şunu gönderir
GET /sse HTTP/1.1 
Host: server.com
Accept: text/event-stream
Sunu şöyle cevaplar
HTTP/1.1 200 OK
Connection: keep-alive Content-Type: text/event-stream Transfer-Encoding: chunked retry: 1000 data: A text message data: {"message": "a JSON message"} event: text data: A message of type 'text' id: 1 event: text data: A message of type 'text' with a unique identifier :ping
Server Sent Events ve WebSocket Farkı Nedir?
Açıklaması şöyle
You can send unidirectional events using the SseEmitter class in Spring. There is already a popular solution available for sending bi-directional events using Websockets. Using WebSockets both server and clients can communicate with each other using bi-directional connections between client and server. SSE is only used for sending uni-directional events from the server to clients using the HTTP protocol.
Açıklaması şöyle
When it comes to SSE, it is often compared to Websockets due to usage similarities between both of the technologies.

- Both are capable of pushing data to the client,
- Websockets are bidirectional – SSE unidirectional,
- In practice, everything that can be done with SSE, and can also be achieved with Websockets,
- SSE can be easier,
- SSE is transported over a simple HTTP connection,
- Websockets require full duplex-connection and servers to handle the protocol,
- Some enterprise firewalls with packet inspection have trouble dealing with Websockets – for SSE that’s not the case,
- SSE has a variety of features that Websockets lack by design, e.g., automatic reconnection, event ids,
- Only Websockets can send both binary and UTF-8 data, SSE is limited to UTF-8, 
- SSE suffers from a limitation to the maximum number of open connections (6 per browser + domain). The issue was marked as Won’t fix in Chrome and Firefox.
Spring İle SSE
Server Sent Events Nedir yazısına taşıdım

Tarayıcı Tarafında 
EventSource kullanılır

Hiç yorum yok:

Yorum Gönder