B/S (Web) Real-time communication solution, web Solution

Source: Internet
Author: User

B/S (Web) Real-time communication solution, web Solution

Real-time communication of B/S is difficult to implement because the http protocol is stateless, making it difficult to implement some functions such as Real-Time Message notification and chat, this article describes several methods that are commonly used before.

 

1. The traditional HTTP protocol is stateless.

The traditional HTTP protocol is stateless. Each request must be initiated by a client (such as a browser). After the server processes the request, the response is returned, it is difficult for the server to actively send data to the client, and it is difficult for multiple users to communicate in real time, such as the chat system.

 

2. Currently, the real-time communication mode of traffic comparison is round-robin.

Condition: Any Web Client

The client regularly sends an Ajax request to the server. After receiving the request, the server immediately returns the response and closes the connection.

 

Long polling

Condition: Any Web Client

The client sends an Ajax request to the server. After receiving the request, the server holds the connection and returns the response information and closes the connection only when a new message exists, after processing the response information, the client sends a new request to the server.

 

Flash socket

Prerequisites: You must install the Flash plug-in.

Embed a Flash program JavaScript using Socket in the page to communicate with the Socket interface on the server by calling the Socket interface provided by the Flash program, after receiving the information sent by the server, JavaScript controls the display of the page.

 

Web Socket

Condition: H5 Web socket is supported.

WebSocket protocol is a new HTML5 protocol. It enables full-duple communication between the browser and the server ). The initial handshake must be completed with HTTP requests.

 

3. B/S real-time communication polling method 3.1 Traditional polling

The client regularly sends an Ajax request to the server. After receiving the request, the server immediately returns the response and closes the connection.

Disadvantage: resource consumption and traffic consumption. The number of requests multiplied by the number of user windows.

 

 

3.2 traditional long polling

The client sends an Ajax request to the server. After receiving the request, the server holds the link and returns the response information and closes the connection only when a new message exists, after processing the response information, the client sends a new request to the server.

Because the browser can only hold a certain number of links at the same time, if multiple windows are opened in this way, the window opened later will always wait for the link held earlier.

 

3.3 traditional long polling + ActiveMq

ActiveMq has some advantages over traditional database queries, which reduces the number of database queries and improves the efficiency. However, in this way, each user's window will have a subscription and link. As a result, the number of links on the ActiveMq server will increase as the user increases, and the number of links will be full.

 

4. ASP. NET SignalR

Obviously, the round-robin method is not the best solution. Of course, web socket is a good implementation method, but programming is troublesome because lz is doing it. net development, so select ASP. NET SignalR. In addition, SignalR is compatible with various browsers. If the browser supports web socket, the web socket method is used. If the browser does not support it, the long polling method is used.

ASP. NET SignalR is a library provided by ASP. NET developers. It simplifies the process for developers to add real-time Web functions to applications. The real-time Web function refers to a function that allows the server code to push content immediately when the connected client becomes available, rather than waiting for the server to wait for the client to request new data. That is, if you have information, let me know again, instead of asking you every time I am dumbfounded.

Http://signalr.net/

 

 

4.1 simple SignalR Programming

The configuration and application details of SignalR can be described in the official parameter documentation. Here, we only provide simple examples of sending and receiving messages.

1. The client sends a message to the server.

2. the server sends a message to the client.

You can select three sending methods when the server sends messages to the client.

1. All: All pages, such as real-time stock information.

Clients. All. xx ();

2. Client: a single page, such as a chat system.

Clients. Client ("connectionId"). xx ();

3. Group: Group. For example, all pages of the same user receive notifications.

Clients. Group ("groupName"). xx ();

 

4.2 SignalR + database

This method solves the problem that the user cannot receive messages in multiple windows, and the refresh frequency of the database only has one thread to refresh and will not increase as the user increases, this greatly reduces the refresh frequency of the database.

 

4.3 SignalR + ActiveMq (final solution)

This method not only solves the problem of the number of links, but also solves the problem that the user cannot receive messages in multiple windows, and does not need to refresh the database in real time when ActiveMq is used.

 

Conclusion

The above is a variety of B/S real-time communication solutions used by lz in development. I hope it will be helpful to you. This article is original and you are welcome to make a selection.Recommendation.

 

 

Related Article

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.