Websocket pushing method supporting capacity expansion
By using the Redis library and Websocket library in the Node.js project, multiple Websocket server instances are created and the connection information is synchronized by using the Redis server, the traditional Websocket service's performance bottleneck under concurrent connections is solved, and efficient data push and good capacity expansion capabilities are achieved.
Patent Information
- Application Number
- CN202411716485.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-03-11
AI Technical Summary
When traditional Websocket services face a large number of concurrent connections, a single server is unbearable, resulting in performance bottlenecks, and how to synchronize and manage Websocket connection status between multiple servers when horizontally scaled becomes a challenge.
By installing the Redis library and Websocket library in the Node.js project, create multiple Websocket server instances, and use the Redis server to store and synchronize Websocket connection information to realize state synchronization and message delivery between Websocket server instances.
It realizes horizontal expansion of Websocket server instances, improves the system's concurrency processing capabilities and availability, ensures reliable delivery of messages between multiple Websocket server instances, and meets the needs of large-scale users' concurrent requests through synchronization and persistence of session states.
Smart Images

Figure CN119603354A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of network communication, and in particular to a Websocket push method supporting capacity expansion. Background Art
[0002] With the growing demand for real-time communication, Websocket, as a full-duplex communication protocol, has been widely used in Web applications.
[0003] However, when faced with a large number of concurrent connections, traditional Websocket services are often difficult for a single server to handle, leading to performance bottlenecks.
[0004] To solve this problem, vertical scaling (increasing the processing power of a single server) or horizontal scaling (increasing the number of servers) is usually adopted. However, vertical scaling has hardware limitations, while horizontal scaling faces the challenge of how to synchronize and manage the Websocket connection status between multiple servers.
[0005] Based on this, the present invention designs a Websocket push method that supports expansion to solve the above problems. Summary of the invention
[0006] In view of the above-mentioned shortcomings of the prior art, the present invention provides a Websocket push method that supports capacity expansion.
[0007] To achieve the above objectives, the present invention is implemented through the following technical solutions:
[0008] A Websocket push method supporting capacity expansion includes the following steps:
[0009] Step 1: Install the Redis library and Websocket library in the Node.js project;
[0010] Step 2: Create one or more Websocket server instances based on the Websocket library, and establish a Redis server based on the Redis library. The Redis server is connected to the Redis client. When establishing a connection, multiple Websocket server instances will store the connection information in the Redis server, and delete it from the Redis server when closing the connection.
[0011] Step 3: The Websocket server instance subscribes to the Redis channel in the Redis server;
[0012] Step 4: The Redis client publishes the information to the specified Redis channel;
[0013] Step 5: The Websocket server instance monitors the information of the corresponding Redis channel in real time and sends the monitored information to the corresponding Websocket client.
[0014] Furthermore, the Redis server is used to store session data and act as a message middleware;
[0015] The Redis server establishes a connection with the Redis client.
[0016] Furthermore, the Websocket client establishes a connection with the Websocket server instance;
[0017] The connection status of all Websocket server instances is stored in the Redis server. When a Websocket client reconnects to a different Websocket server instance, the Websocket server instance obtains the session information of the Websocket client from the Redis server through the session synchronization module, thereby restoring the connection status with the Websocket client.
[0018] Furthermore, the implementation process of step 2 includes:
[0019] A1. Initialize the Redis client and subscribe to the specified Redis channel;
[0020] A2. Create multiple Websocket server instances and establish a monitoring Redis channel connection request;
[0021] A3. When multiple Websocket server instances are connected, the connection information is stored in the Redis server;
[0022] A4. When multiple Websocket server instance connections are closed, the corresponding connection information is deleted from the Redis server.
[0023] Furthermore, a Websocket server instance subscribes to one or more Redis channels in the Redis server to receive messages.
[0024] Furthermore, the Redis client includes the Redis publisher and the Redis subscriber.
[0025] Furthermore, the Redis publisher sends the monitoring information to the Redis subscriber through the Redis channel, and then the Redis subscriber sends it to the Websocket server instance.
[0026] Furthermore, the connection information includes a session ID and user information.
[0027] Furthermore, the method of pushing capacity expansion of Websocket server instances is to increase the number of Websocket server instances.
[0028] Furthermore, the Websocket server connects to the Websocket client via a URL.
[0029] Compared with the prior art, the present invention has the following beneficial effects: 1. The present invention realizes the horizontal expansion of Websocket server instances, thereby improving the concurrent processing capability and availability of the system;
[0030] 2. The present invention realizes reliable transmission of messages between multiple Websocket server instances through the subscription or publishing mechanism of the Redis server;
[0031] 3. The present invention stores session information in a Redis server, thereby achieving synchronization and persistence of session status.
[0032] 4. The Websocket push method of the present invention can realize efficient data push and has good expansion capability. Through the subscription management of the Redis server, the Websocket server instance connection object and the load balancing strategy can meet the needs of large-scale user concurrent requests and improve the stability and reliability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0034] Figure 1 A flowchart of a Websocket push method supporting capacity expansion according to the present invention;
[0035] Figure 2 A connection frame for a Websocket push method supporting capacity expansion according to the present invention Figure 1 ;
[0036] Figure 3 It is a schematic block diagram of the Redis server of the present invention;
[0037] Figure 4 It is a schematic block diagram of the Redis client of the present invention;
[0038] Figure 5A connection frame for a Websocket push method supporting capacity expansion according to the present invention Figure 2 . DETAILED DESCRIPTION
[0039] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0040] Embodiment 1: In some embodiments, please refer to the drawings of the specification Figure 1-Figure 5 , including the following steps:
[0041] Step 1: Install the Redis library and Websocket library in the Node.js project;
[0042] Node.js was released in May 2009 and developed by Ryan Dahl. It is a JavaScript runtime environment based on the Chrome V8 engine. It uses an event-driven, non-blocking I / O model to allow JavaScript to run on the server-side development platform. It makes JavaScript a scripting language on par with server-side languages such as PHP, Python, Perl, and Ruby.
[0043] Redis (Remote Dictionary Server) is an open source log-type, Key-Value database written in ANSI C language, supporting the network, memory-based and persistent, and providing APIs in multiple languages.
[0044] Websocket (two-way communication) is a full-duplex communication protocol on a single TCP connection. It allows real-time, two-way data exchange between clients and servers without the need to frequently initiate new connection requests. The Websocket protocol was defined as the RFC6455 standard by the IETF in 2011 and supplemented by RFC7936. Websocket makes data exchange between clients and servers simpler, and the server can actively push data to the client.
[0045] Step 2: Create one or more Websocket server instances based on the Websocket library, and establish a Redis server based on the Redis library. The Redis server is connected to the Redis client. When establishing a connection, multiple Websocket server instances store the connection information (such as session ID, user information, etc.) in the Redis server, and delete it from the Redis server when closing the connection.
[0046] The implementation process of step 2 includes:
[0047] A1. Initialize the Redis client and subscribe to the specified Redis channel;
[0048] A2. Create multiple Websocket server instances and establish a monitoring Redis channel connection request;
[0049] A3. When multiple Websocket server instances are connected, the connection information is stored in the Redis server;
[0050] A4. When multiple Websocket server instance connections are closed, the corresponding connection information is deleted from the Redis server.
[0051] Redis server, used to store session data and serve as message middleware; Redis server establishes a connection with Redis client.
[0052] The Redis client includes the Redis publisher and the Redis subscriber.
[0053] The Websocket client establishes a connection with the Websocket server instance;
[0054] A Websocket server instance subscribes to one or more Redis channels in the Redis server to receive messages.
[0055] The Redis publisher sends the monitoring information to the Redis subscriber through the Redis channel, and then the Redis subscriber sends it to the Websocket server instance.
[0056] The connection status of all Websocket server instances is stored in the Redis server. When a Websocket client reconnects to a different Websocket server instance, the Websocket server instance obtains the session information of the Websocket client from the Redis server through the session synchronization module, thereby restoring the connection status with the Websocket client.
[0057] Method for pushing capacity expansion of Websocket server instances: Increase the number of Websocket server instances.
[0058] The Websocket server connects with the Websocket client via a URL.
[0059] All newly added server instances will subscribe to the same Redis channel and be able to obtain session information from the Redis server.
[0060] URL (Uniform Resource Locator) is a uniform resource locator, which is the address of a standard resource on the Internet. The format of a URL usually consists of a protocol type, a host name, a path, and a query string. The protocol type (such as http, https, ftp, etc.) specifies the access method that the browser should use. The host name is the name of the server or website. The path specifies the specific location of the resource on the server. The query string is used to pass parameters to the server.
[0061] URL is used to specify the specific location of resources on the Internet. Each resource has a unique URL. In Web development, URL is also called URLconf, which is used to define the access path of a web page and the corresponding processing function or page. Through the URL, users can access various resources on the Internet, such as web pages, pictures, videos, etc.
[0062] Simply put, URL is a web address. Each address represents a different web page or resource. Users access these resources by entering the URL in the browser.
[0063] Step 3: The Websocket server instance subscribes to the Redis channel in the Redis server;
[0064] Step 4: The Redis client publishes the information to the specified Redis channel;
[0065] Step 5: The Websocket server instance monitors the information of the corresponding Redis channel in real time and sends the monitored information to the corresponding Websocket client.
[0066] When the present invention is implemented, when a message is sent to a Websocket client, the message is first published to the corresponding Redis channel of the Redis server, and all Websocket server instances subscribed to the channel will receive the message and send the message to the corresponding Websocket client according to the message content.
[0067] The Websocket push method of the present invention can realize efficient data push and has good expansion capability. Through the subscription management of the Redis server, the Websocket server instance connection object and the load balancing strategy can meet the needs of large-scale user concurrent requests and improve the stability and reliability of the system.
[0068] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements will not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A Websocket push method supporting capacity expansion, characterized in that: The following steps are involved: Step 1: Install the Redis library and Websocket library in the Node.js project; Step 2: Create one or more Websocket server instances based on the Websocket library, and create a Redis server based on the Redis library. The Redis server is connected to the Redis client. Multiple Websocket server instances store the connection information in the Redis server when establishing a connection, and delete it from the Redis server when closing the connection; Step 3: The Websocket server instance subscribes to the Redis channel in the Redis server; Step 4: The Redis client publishes the information to the specified Redis channel; Step 5: The Websocket server instance monitors the information of the corresponding Redis channel in real time and sends the monitored information to the corresponding Websocket client.
2. The Websocket push method supporting expansion according to claim 1, characterized in that: Redis server, used to store session data and serve as a message middleware. The Redis server establishes a connection with the Redis client.
3. The Websocket push method supporting capacity expansion according to claim 2, characterized in that: The Websocket client establishes a connection with the Websocket server instance; The connection status of all Websocket server instances is stored in the Redis server. When a Websocket client reconnects to a different Websocket server instance, the Websocket server instance obtains the session information of the Websocket client from the Redis server through the session synchronization module, thereby restoring the connection status with the Websocket client.
4. The Websocket push method supporting capacity expansion according to claim 3, characterized in that: The implementation process of step 2 includes: A1. Initialize the Redis client and subscribe to the specified Redis channel; A2. Create multiple Websocket server instances and establish a monitoring Redis channel connection request; A3. When multiple Websocket server instances are connected, the connection information is stored in the Redis server; A4. When multiple Websocket server instance connections are closed, the corresponding connection information is deleted from the Redis server.
5. The Websocket push method supporting expansion according to claim 4, characterized in that: A Websocket server instance subscribes to one or more Redis channels in the Redis server to receive messages.
6. The Websocket push method supporting capacity expansion according to claim 5, characterized in that: The Redis client includes the Redis publisher and the Redis subscriber.
7. The Websocket push method supporting expansion according to claim 6, characterized in that: The Redis publisher sends the monitoring information to the Redis subscriber through the Redis channel, and then the Redis subscriber sends it to the Websocket server instance.
8. The Websocket push method supporting expansion according to claim 7, characterized in that: The connection information includes session ID and user information.
9. The Websocket push method supporting expansion according to claim 8, characterized in that: Method for pushing capacity expansion of Websocket server instances: Increase the number of Websocket server instances.
10. The Websocket push method supporting capacity expansion according to claim 9, characterized in that: The Websocket server connects with the Websocket client via a URL.