Home Web Front-end H5 Tutorial Detailed graphic explanation of HTML5 data push SSE principle and application development

Detailed graphic explanation of HTML5 data push SSE principle and application development

Mar 08, 2017 pm 03:23 PM

JavaScript expresses behavior and CSS expresses appearance. Note that HTML expresses both structure (logical structure) and content (data itself). Usually when data needs to be updated, the structure does not need to be updated. This is exactly what happens. The desire to change only the data without changing the organizational structure has promoted the emergence of data pull and data push technologies.

SSE is an HTML5 technology that allows the server to push new data to the client (referred to as data push). There are two alternatives to data push: no updates and data pull.

No update solution:

After loading the HTML, you will get an HTML page, and then the browser will request images, CSS files, JavaScript files, etc., they They are all static files that the browser can cache. If the page uses a back-end language, such as PHP, Ruby, Python and other languages ​​that dynamically generate HTML for users.

Data pull scheme:

The browser will send data to the browser based on some user behavior, or after a certain period of time, or based on some other trigger scheme. The server side requests or updates all data, and can command the entire page to reload through JavaScript or a meta tag. The familiar Ajax technology is only used to request the latest data. When the data is received, the javascript function will use it to locally update the DOM. The essence of data pulling: only pull new data, and only update the affected parts of the page.

None of the above is data push. Data push is not a static file, nor does it involve the browser initiating a request to update data. Data push is when the server selects the client to send new data.

When the data source has new data, the server can immediately send it to one or more clients without waiting for the client to request. These new data may be unexpected. Post news, latest stocks, chat messages from online friends, new weather forecasts, next steps in strategy games, etc.

SSE is suitable for frequent updates, low latency and data from the server to the client. The difference between it and WebSocket:

1) Convenience. No need to add any new components. You can continue to use it with any back-end language and framework you are used to. There is no need to get a new IP or a new virtual machine for a new virtual machine. Trouble with the port number.

2) Simplicity on the server side. Because SSE operates on existing HTTP/HTTPS protocols, it can run directly on existing proxy servers and authentication technologies.

The biggest advantage of WebSocket over SSE is that it is a two-way communication, which means that sending data to the server is as simple as receiving data from the server, while SSE generally passes a separate Ajax request from the client to the server. Transfer data, so using Ajax will increase overhead compared to WebSocket. Therefore, if you need to transmit data to the server once per second or faster, you should use WebSocket.

The specific code is as follows:

html code

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>basic SSE test</title>
    </head>
    <body>
        <pre id = "x">initializting...
<script> var es = new EventSource("basic_sse.php"); es.addEventListener("message",function(e){ //e.data document.getElementById("x").innerHTML += "\n"+e.data; },false);//使用false表示在冒泡阶段处理事件,而不是捕获阶段。 </script>
Copy after login

It should be noted that: use the server It is best to check before entering the data to prevent potential JavaScript injection attacks.

php code

<?php
    header(&#39;Content-Type: text/event-stream&#39;);
    header(&#39;Cache-Control: no-cache&#39;);
    $time = date(&#39;r&#39;);
    echo "data: The server time is: {$time}\n\n";
    flush();
?>
Copy after login

"Content-Type: text/event-stream" is specially designed for SSE MIME type,

Effect screenshot

When data push is the wrong choice

Consider static first In this case, without introducing data push, every time the user opens a page, a socket connection will be opened between the browser and the server, and the server phone information is then returned to the user. It may be as simple as loading a page from the disk. Just like a static HTML file or an image, it can be complex, like running a background language that connects to many databases. The key point here is that once the required information is returned, the socket is closed and each HTTP request opens one of these relatively short-lived socket connections, which are limited on the server. Resources, whenever they complete their intended tasks, are recovered for recycling.

Now let’s compare data push. A request never completes, there is always a lot of information to send, so the socket remains open. Obviously, because they are limited resources, there will be a limit to the number of SSE connections at the same time.

Imagine a situation where you are providing telephone service support for your latest application. There are 10 call center employees serving 1,000 users. The user encounters a problem and one of the operators picks up the line and then hangs up. New customer calls are queued until one of the operators hangs up, which is a typical network service pattern.

However, now a customer called and said, I have no problem now, but I will use your product in the next few hours, and if I encounter any problem, I hope you will reply immediately. This customer will stay on the phone with the operator for several hours, and 10% of the call center's service resources are wasted. If there are 10 such customers, then the other 990 customers cannot call. This is the data push model.

Of course, this is not always a bad thing. If this customer has a problem every few seconds throughout the afternoon, keeping the phone open will not waste 10% of service resources, but will increase it. Because each question requires a new call (like a data pull), operators need to spend extra time verifying the customer's identity and pulling up accounts, reducing service efficiency. Staying on the phone usually not only makes customers more satisfied, but also improves the efficiency of the call center. This is the most suitable scenario for data push.


The above is the detailed content of Detailed graphic explanation of HTML5 data push SSE principle and application development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles