WebSocket-based message pushing method under micro-service architecture
By using Redis to register WebSocket instance information and heartbeat detection mechanism under the microservice architecture, the problem of inconvenient message transmission caused by WebSocket connection in a multi-instance environment is solved, efficient front-end and back-end communication is achieved, and customer service response speed and user satisfaction are improved.
Patent Information
- Application Number
- CN202410615320.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-17
- Publication Date
- 2024-09-06
AI Technical Summary
Under the microservice architecture, the stateful nature of WebSocket connections leads to inconvenience in message delivery. Especially in multi-instance environments, it is difficult to accurately identify the service instance where the connection is located, resulting in low message delivery efficiency and increased server load.
By starting multiple WebSocket servers in the back-end microservices, registering instance information with Redis, establishing a WebSocket connection between the front-end and the back-end, maintaining the heartbeat detection and automatic reconnection mechanism, generating channel IDs for message push, and using Redis to find the target instance IP to achieve message synchronization.
It achieves efficient front-end and back-end communication in a distributed microservice architecture, improves the accuracy and efficiency of message delivery, reduces server load, and significantly improves customer service response speed and user satisfaction.
Smart Images

Figure CN118612199A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a message pushing method based on WebSocket in a microservice architecture. Background Art
[0002] In order to achieve real-time text chat communication in the browser, the traditional way is to use HTTP long polling for the browser to send a request to the backend server, and the server keeps the request open until there is a new message to send.
[0003] With HTTP long polling, each request requires a new HTTP connection to be established, which increases latency and server load. At the same time, due to over-reliance on the browser to periodically initiate requests, message delivery may be delayed.
[0004] WebSocket provides a full-duplex communication channel, allowing data to be transmitted in both directions at the same time. This means that the server and client can send and receive information at the same time, improving communication efficiency. In contrast, HTTP long polling is still based on HTTP's request-response model. Although it can simulate real-time communication, it is still half-duplex in nature. At the same time, the WebSocket communication protocol is more efficient than HTTP long polling because after establishing a connection, data exchange no longer requires redundant HTTP header information, reducing additional bandwidth consumption and processing overhead.
[0005] The stateful nature of WebSocket brings some challenges and problems in a microservice architecture. In a microservice architecture, a load balancer is usually used to distribute requests to different service instances. Since WebSocket connections are stateful, once a client establishes a WebSocket connection with a service instance, the connection must maintain communication with this specific instance. In this case, the server needs to provide a source search mechanism to ensure that the service instance where the connection is located can be accurately identified in the microservice architecture to deliver messages. Summary of the invention
[0006] In order to overcome the shortcomings of the prior art, the present invention provides a message push method based on WebSocket in a microservice architecture. The scheme is suitable for the situation where multiple instances of backend services run in a distributed microservice architecture system, and realizes efficient communication with front-end services.
[0007] To achieve the above purpose, a message push method based on WebSocket under the microservice architecture is designed, which is characterized by: the specific process is as follows: Step 1: The backend microservice starts multiple WebSocket servers and registers instance information in Redis; Step 2: After the user logs in to the system, the front-end page will initiate a request to the server, create a WebSocket connection, and establish a connection with the back-end service to maintain the heartbeat detection and automatic reconnection mechanism of the connection; Step 3: The user enters content in the chat box, and the front-end encapsulates the message into JSON data and pushes it to the back-end server through WebSocket; Step 4: After the user message is pushed to the backend, the backend server searches for the backend server where the corresponding WebSocket connection is located according to the ID of the person being pushed, and sends the event to the corresponding backend server.
[0008] The specific process of step 1 also includes: (11) The backend server uses the Netty framework to establish and manage WebSocket connections. When the service instance node is started, it specifies the interface and port that the service instance node listens on and is ready to accept client connections. (12) WebSocket is a stateful two-way connection. When starting multiple instances, it is necessary to record the server instance connected. After each instance is successfully started, register the IP and port of the instance with Redis. (13) The back-end server provides a unified message processing mechanism to parse different types of message formats.
[0009] It supports users to establish multiple connections to the backend server instances from multiple channels at the same time. If the user opens multiple browser pages, or opens them from a PC browser or a mobile browser respectively, separate connections will be established. The backend server will generate a different channelID based on each connection established to indicate the connection activity.
[0010] When a user establishes multiple connections at the same time, it is necessary to ensure that each message sent or received is also synchronized on the other end. When the backend server pushes a message to the user, it will simultaneously push the message to all active connections of the user in different instances.
[0011] The specific process of step 2 also includes: (21) When the user opens the chat window page, the front-end service creates a WebSocket connection with a heartbeat detection function to keep the connection active; (22) Front-end service initialization event handler, including onopen, onmessage, onerror, and onclose methods; (23) After the connection is established through onopen, the connection is kept active by sending ping messages periodically. If no ping response is received within the specified timeout, an error is triggered and a reconnection attempt may be initiated; (24) After the connection is established, if an exception occurs in the connection, an error handling mechanism is provided, including logging and triggering reconnection when an error occurs, and attempting to reconnect when the connection is lost; (25) After the front-end and back-end connection is successfully established, the back-end service will generate a channelID based on the current connection, and store the current user's ID and channelID as a combined unique key in the cache of the current instance. At the same time, the user connection will also be registered in Redis.
[0012] The specific process of step 3 also includes: (31) When the user opens the chat window page in the web browser, enters content and sends a message, the front-end service executes the onmessage method, encapsulates the content sent by the user into JSON format, and communicates through the WebSocket protocol; (32) When the back-end server receives the message, it further processes the message sent by the user through a unified interpreter.
[0013] The specific process of step 4 also includes: (41) The backend server instance identifies the source user ID of the message push and the ID list of multiple target users to be pushed; (42) Loop through the list of user IDs that need to be pushed; (43) In each loop, in the current instance cache, check whether the user's channelID is cached according to the user ID. If found, call the push method to send it; (44) If not found, look up the server instance IP address actually cached by the user connection in Redis and send an event to the instance; (45) Other service instances receive the event, look up the user's channelID in their own cache based on the user ID, find the connection, and call the push method to send the message.
[0014] Compared with the prior art, the present invention provides a message push method based on WebSocket under a microservice architecture. The scheme is suitable for the situation where a backend service runs in multiple instances in a distributed microservice architecture system, and realizes efficient communication with a frontend service.
[0015] This solution can significantly improve the response speed and work efficiency of Yanqian Cloud platform customer service, thereby improving user satisfaction and loyalty, and effectively promoting business development and market competitiveness. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 It is the work flow chart of the present invention.
[0017] Figure 2 It is the workflow diagram of step 3 of the present invention.
[0018] Figures 3 to 6 It is a schematic diagram of a specific case of the present invention. DETAILED DESCRIPTION
[0019] The present invention will be further described below with reference to the accompanying drawings.
[0020] like Figure 1 As shown in the figure, a message push method based on WebSocket in a microservice architecture is shown in the figure. The specific process is as follows: Step 1: The backend microservice starts multiple WebSocket servers and registers instance information in Redis; Step 2: After the user logs in to the system, the front-end page will initiate a request to the server, create a WebSocket connection, and establish a connection with the back-end service to maintain the heartbeat detection and automatic reconnection mechanism of the connection; Step 3: The user enters content in the chat box, and the front-end encapsulates the message into JSON data and pushes it to the back-end server through WebSocket; Step 4: After the user message is pushed to the backend, the backend server searches for the backend server where the corresponding WebSocket connection is located according to the ID of the person being pushed, and sends the event to the corresponding backend server.
[0021] The specific process of step 1 also includes: (11) The backend server uses the Netty framework to establish and manage WebSocket connections. When the service instance node is started, it specifies the interface and port that the service instance node listens on and is ready to accept client connections. (12) WebSocket is a stateful two-way connection. When starting multiple instances, it is necessary to record the server instance connected. After each instance is successfully started, register the IP and port of the instance with Redis. (13) The back-end server provides a unified message processing mechanism to parse different types of message formats.
[0022] It supports users to establish multiple connections to the backend server instances from multiple channels at the same time. If the user opens multiple browser pages, or opens them from a PC browser or a mobile browser respectively, separate connections will be established. The backend server will generate a different channelID based on each connection established to indicate the connection activity.
[0023] When a user establishes multiple connections at the same time, it is necessary to ensure that each message sent or received is also synchronized on the other end. When the backend server pushes a message to the user, it will simultaneously push the message to all active connections of the user in different instances.
[0024] The specific process of step 2 also includes: (21) When the user opens the chat window page, the front-end service creates a WebSocket connection with a heartbeat detection function to keep the connection active; (22) Front-end service initialization event handler, including onopen, onmessage, onerror, and onclose methods; (23) After the connection is established through onopen, the connection is kept active by sending ping messages periodically. If no ping response is received within the specified timeout, an error is triggered and a reconnection attempt may be initiated; (24) After the connection is established, if an exception occurs in the connection, an error handling mechanism is provided, including logging and triggering reconnection when an error occurs, and attempting to reconnect when the connection is lost; (25) After the front-end and back-end connection is successfully established, the back-end service will generate a channelID based on the current connection, and store the current user's ID and channelID as a combined unique key in the cache of the current instance. At the same time, the user connection will also be registered in Redis.
[0025] like Figure 2 As shown, the specific process of step 3 also includes: (31) When the user opens the chat window page in the web browser, enters content and sends a message, the front-end service executes the onmessage method, encapsulates the content sent by the user into JSON format, and communicates through the WebSocket protocol; (32) When the back-end server receives the message, it further processes the message sent by the user through a unified interpreter.
[0026] The specific process of step 4 also includes: (41) The backend server instance identifies the source user ID of the message push and the ID list of multiple target users to be pushed; (42) Loop through the list of user IDs that need to be pushed; (43) In each loop, in the current instance cache, check whether the user's channelID is cached according to the user ID. If found, call the push method to send it; (44) If not found, look up the server instance IP address actually cached by the user connection in Redis and send an event to the instance; (45) Other service instances receive the event, look up the user's channelID in their own cache based on the user ID, find the connection, and call the push method to send the message.
[0027] The implementation cases of the present invention are as follows: like Figure 3 , Figure 4 As shown, users can access customer service in the client of the chat window, join group chat, and chat.
[0028] like Figure 5 , Figure 6 As shown, customer service personnel can enter the customer service center, chat with users, answer users' questions, and provide support services.
[0029] The solution of the present invention is applicable to the situation where multiple instances of backend services are running in a distributed microservice architecture system, and realizes efficient communication with frontend services.
[0030] The core content includes: establishing a WebSocket connection between the front-end and back-end servers, maintaining the heartbeat detection and automatic reconnection mechanism of the connection, when the user sends a message through the front-end server, the message is forwarded to the back-end, and the back-end server finds the corresponding WebSocket connection according to the user ID and pushes the message; supports users to establish multiple connections from multiple channels at the same time, and supports multi-end message synchronization. Based on this, a fast, efficient, reliable and user-friendly customer service communication platform is provided.
[0031] This technology can significantly improve the response speed and work efficiency of Yanqian Cloud platform customer service, thereby improving user satisfaction and loyalty, and effectively promoting business development and market competitiveness.
Claims
1. A message push method based on WebSocket under a microservice architecture, characterized in that: The specific process is as follows: Step 1: The backend microservice starts multiple WebSocket servers and registers instance information in Redis; Step 2: After the user logs in to the system, the front-end page will initiate a request to the server, create a WebSocket connection, and establish a connection with the back-end service to maintain the heartbeat detection and automatic reconnection mechanism of the connection; Step 3: The user enters content in the chat box, and the front-end encapsulates the message into JSON data and pushes it to the back-end server through WebSocket; Step 4: After the user message is pushed to the backend, the backend server searches for the backend server where the corresponding WebSocket connection is located according to the ID of the person being pushed, and sends the event to the corresponding backend server.
2. According to claim 1, a message push method based on WebSocket under a microservice architecture is characterized in that: The specific process of step 1 also includes: (11) The backend server uses the Netty framework to establish and manage WebSocket connections. When the service instance node is started, it specifies the interface and port that the service instance node listens on and is ready to accept client connections. (12) WebSocket is a stateful two-way connection. When starting multiple instances, it is necessary to record the server instance connected. After each instance is successfully started, register the IP and port of the instance with Redis. (13) The back-end server provides a unified message processing mechanism to parse different types of message formats.
3. According to claim 2, a message push method based on WebSocket under a microservice architecture is characterized in that: It supports users to establish multiple connections to the backend server instances from multiple channels at the same time. If the user opens multiple browser pages, or opens them from a PC browser or a mobile browser respectively, separate connections will be established. The backend server will generate a different channelID based on each connection established to indicate the connection activity.
4. According to a method for pushing messages based on WebSocket in a microservice architecture as claimed in claim 2, it is characterized in that: When a user establishes multiple connections at the same time, it is necessary to ensure that each message sent or received is also synchronized on the other end. When the backend server pushes a message to the user, it will simultaneously push the message to all active connections of the user in different instances.
5. According to a method for pushing messages based on WebSocket in a microservice architecture as claimed in claim 1, it is characterized in that: The specific process of step 2 also includes: (21) When the user opens the chat window page, the front-end service creates a WebSocket connection with a heartbeat detection function to keep the connection active; (22) Front-end service initialization event handler, including onopen, onmessage, onerror, and onclose methods; (23) After the connection is established through onopen, the connection is kept active by sending ping messages periodically. If no ping response is received within the specified timeout, an error is triggered and a reconnection attempt may be initiated; (24) After the connection is established, if an exception occurs in the connection, an error handling mechanism is provided, including logging and triggering reconnection when an error occurs, and attempting to reconnect when the connection is lost; (25) After the front-end and back-end connection is successfully established, the back-end service will generate a channelID based on the current connection, and store the current user's ID and channelID as a combined unique key in the cache of the current instance. At the same time, the user connection will also be registered in Redis.
6. According to a method for pushing messages based on WebSocket in a microservice architecture as claimed in claim 1, it is characterized in that: The specific process of step 3 also includes: (31) When the user opens the chat window page in the web browser, enters content and sends a message, the front-end service executes the onmessage method, encapsulates the content sent by the user into JSON format, and communicates through the WebSocket protocol; (32) When the back-end server receives the message, it further processes the message sent by the user through a unified interpreter.
7. According to claim 1, a message push method based on WebSocket under a microservice architecture is characterized in that: The specific process of step 4 also includes: (41) The backend server instance identifies the source user ID of the message push and the ID list of multiple target users to be pushed; (42) Loop through the list of user IDs that need to be pushed; (43) In each loop, in the current instance cache, check whether the user's channelID is cached according to the user ID. If found, call the push method to send it; (44) If not found, look up the server instance IP address actually cached by the user connection in Redis and send an event to the instance; (45) Other service instances receive the event, look up the user's channelID in their own cache based on the user ID, find the connection, and call the push method to send the message.
Citation Information
Cited By
Service cluster websocket connection and message distribution method under micro-service architecture
CN120281805A
