Implement Web message push technology and adopt long polling Corundumstudio introduction

Source: Internet
Author: User
Tags button type

What technologies are available for Web message push:

  • constant polling (commonly known as "pull", polling) is a means of acquiring real-time messages: Ajax interval (usually using JavaScript's setTimeout function) goes to the server to see if there is a change, so that the incremental update. But how long is the interval to query becomes a problem, because performance and immediacy cause a serious inverse relationship. The interval is too short, and a continuous request can break the server, the interval is too long, and the new data on it takes more time to reach the client.

    • Advantages: The service-side logic is simple;

    • Disadvantage: Most of these requests may be invalid requests, and the pressure on the server is very high when a large number of users poll frequently;

    • Application: The number of concurrent users, and the demand for real-time information is not high, generally rarely used;

  • Long Polling Technology (long-polling): The client sends an AJAX request to the server, the server receives a request to hold the connection until a new message or timeout (setting) returns the response information and closes the connection, and the client processes the response information before sending a new request to the server.

    • Advantages: High real-time, without the message of the case will not make frequent requests;

    • Disadvantage: The server will consume resources while maintaining the connection;

  • iframe and Htmlfile-based streaming (streaming) : The iframe stream is a hidden iframe inserted into the page, using its SRC attribute to create a long link between the server and the client. The server transmits data to an IFRAME (usually HTML, with JavaScript in charge of inserting information) to update the page in real time.

    • Advantage: The message can arrive in real time;

    • Disadvantage: The server maintains a long connection period will consume resources;

  • plug-in provides socket mode : such as the use of Flash Xmlsocket,java applet socket interface, ActiveX packaging socket.

    • Advantages: The native socket support, and the PC end and the mobile end of the implementation of a similar way;

    • Disadvantage: The browser side needs to install the appropriate plug-in;

  • WebSocket: A network technology that HTML5 started to provide full duplex communication between the browser and the server.

      • Advantages: Better save server resources and bandwidth and achieve real-time communication;

      • Disadvantages: Not yet popular, browser support is not good;

In summary, the use of long polling (long-polling) is a good way to consider browser compatibility and performance issues.

Netty-socketio is an open source Socket.io server-side implementation of Java, which is based on the Netty framework. The project address is: Https://github.com/mrniko/netty-socketio

The following is a push example from Netty-socketio, a web chat system.

Server-side Push server:

Message entity:

View Code

Listener events:

Package Com.nettysocketio.test;import Com.corundumstudio.socketio.ackrequest;import Com.corundumstudio.socketio.socketioclient;import Com.corundumstudio.socketio.socketioserver;import Com.corundumstudio.socketio.listener.datalistener;public class Charteventlistener implements datalistener< chatobject> {    socketioserver server;    public void Setserver (Socketioserver server) {        this.server = server;    }    public void OnData (socketioclient client, chatobject data,            ackrequest Acksender) throws Exception {        // Chatevent is the name of the event, data is the content sent        this.server.getBroadcastOperations (). Sendevent ("chatevent", data);}    }

Push service:

Package Com.nettysocketio.test;import Com.corundumstudio.socketio.configuration;import Com.corundumstudio.socketio.socketioserver;public class App {public    static void Main (string[] args) throws Interruptedexception    {        configuration config = new configuration ();        Config.sethostname ("localhost");        Config.setport (9092);        Socketioserver Server = new Socketioserver (config);        Charteventlistener listner = new Charteventlistener ();        Listner. Setserver (server);        Chatevent is the event name        Server.addeventlistener ("Chatevent", Chatobject.class, Listner);        Start Service        Server.start ();        Thread.Sleep (integer.max_value);        Server.stop ();    }}

Browser-side client:

The client is using Socket.io-client, and the project address is: https://github.com/Automattic/socket.io-client

Client Chat Code:

Recently, a project developed by the company, one of which is based on bpm2.0 process tasks, is used for message processing.

<! DOCTYPE html>

Run Push server first, and then open chat HTML to see the connection information and the server push chat message.

Reference:

Https://github.com/mrniko/netty-socketio

Https://github.com/mrniko/netty-socketio-demo

Implement Web message push technology and adopt long polling Corundumstudio introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.