The best HTML5 Server-Sent Events (Server-Sent Events) Tutorial In 2024, In this tutorial you can learn Server-Sent Events - way messaging,Browser Support,Receiving Server-Sent Event Notification,Examples,Detection Server-Sent Events Support,Examples of server-side code,Examples,EventSource objects,
HTML5 server send events (server-sent event) allows web pages to get updates from the server.
Server-Sent event is the website automatically obtain updates from the server.
Previously may also do so, provided that the page had to ask if an update is available. The server sends the event, the update will arrive automatically.
Examples: Facebook / Twitter updates, valuation updates, new blog, tournament results and so on.
All major browsers are supported by the server sends the event, in addition to Internet Explorer.
EventSource object is used to receive event notifications sent by the server:
Analysis examples:
The following examples, we write some additional code to detect the browser to the server to send events supported by:
To make the example above, you can run, you also need to be able to send data to update the server (such as PHP and ASP).
Syntax server-side event stream is very simple. The "Content-Type" header is set to "text / event-stream". You can now start sending the event flow.
ASP code (VB) (demo_sse.asp):
Code explanation:
In the above example, we used to get the message onmessage event. However, you can also use other events:
事件 | 描述 |
---|---|
onopen | 当通往服务器的连接被打开 |
onmessage | 当接收到消息 |
onerror | 当发生错误 |