HTML SSE API

Server-Sent Events (SSE) allow a website to receive updates from a server.

Server-Sent Events (SSE) allow a website to receive updates from a server .

 

Server-Sent Events - One-way messaging

Server-Sent Events are when a website automatically receives updates from the server.

This could have happened before, but the website would have had to ask if there were any updates. With Server-Sent Events, updates arrive automatically.

For example, Facebook/Twitter updates, stock price updates, news feed, sports results, etc.

Browser support

The numbers in the table indicate the browser version that first fully supported Server-Sent Events.

API
SSE
Google Chrome MS Edge Firefox Safari Opera
6.0 79.0 6.0 5.0 11.5

Receive Server-Sent Event notifications

The object EventSourceused to receive event notifications sent by the server:

For example:

var source = new EventSource("demo_sse.php"); source.onmessage = function(event) { document.getElementById("result").innerHTML += event.data + "
"; };

The example is explained as follows:

  • Create a new object EventSourceand specify the URL of the page that submits updates (in this example, " demo_sse.php ").
  • Every time an update is received, the onmessage event will occur.
  • When the onmessage event occurs, place the received data into the element with the id="result"

Check Server-Sent Events support

In the example above, there are some additional lines of code to check browser support for Server-Sent Events:

if(typeof(EventSource) !== "undefined") { // Yes! Server-sent events support! // Some code. } else { // Sorry! No server-sent events support. }

 

Example of server-side code

For the above example to work, you need a server capable of sending data updates (such as PHP or ASP).

Server-side event stream syntax is very simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending event streams.

Code in PHP (demo_sse.php):

 

Code in ASP (VB) (demo_sse.asp):

<% Response.ContentType = "text/event-stream" Response.Expires = -1 Response.Write("data: The server time is: " & now()) Response.Flush() %>

The code is explained as follows:

  • Set the "Content-Type" header to "text/event-stream".
  • Specify that the page should not be cached.
  • Export data for sending (Always start with "data:" )
  • Return the output data to the website.

EventSource object

In the examples above, the author used the onmessage event to receive the message. But other events are also available:

Sự kiện Mô tả
onopen Khi kết nối với máy chủ được mở
onmessage Khi một tin nhắn được nhận
onerror Khi xảy ra lỗi
Related posts
Other HTML articles
  • HTML Web Workers API

    web worker là một javascript chạy ở chế độ nền, không ảnh hưởng đến hiệu suất của trang.
  • HTML Web Storage API

    lưu trữ web an toàn hơn và một lượng lớn dữ liệu có thể được lưu trữ cục bộ mà không ảnh hưởng đến hiệu suất trang web.
  • HTML Drag & Drop API

    trong html, bất kỳ phần tử nào cũng có thể được kéo và thả.
  • HTML Geolocation API

    html geolocation api được sử dụng để xác định vị trí của người dùng.
  • YouTube video in HTML

    cách dễ nhất để phát video bằng html là sử dụng youtube.
  • Inserting video in HTML5

    cách chèn video vào html như thế nào? nếu đang muốn tìm hiểu cách chèn video vào website html, mời bạn tham khảo bài viết dưới đây.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup