WebSocket-based Golang and Redux state real-time synchronization and persistence method and device and electronic equipment
Through a WebSocket-based method, combined with Redux and Golang, real-time synchronization and persistence of server-side and front-end states is achieved, the problem of complex middleware dependencies in the existing technology is solved, real-time and consistency are improved, and the development process is simplified.
Patent Information
- Application Number
- CN202411884645.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-04-15
AI Technical Summary
The existing technology is difficult to realize real-time synchronization and persistence of the Golang server and front-end Redux state, and relies on complex middleware or third-party libraries, which increases development and maintenance costs.
Using WebSocket-based methods, client state management is carried out through Redux, and WebSocket state manager, global state container and database interface are implemented on the Golang server to realize real-time state monitoring, update and persistence.
Real-time synchronization and persistence of Golang server and front-end Redux state, significantly improving user experience and data consistency, simplifying the front-end synchronization logic, and reducing development and maintenance costs.
Smart Images

Figure CN119829678A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of real-time data synchronization, and in particular to a method, device and electronic device for real-time synchronization and persistence of Golang and Redux states based on WebSocket. Background Art
[0002] In modern Web application development, data synchronization and state management between the front-end and the back-end are very critical links. The traditional HTTP request-response model usually has latency problems and cannot reflect data changes in real time. This is particularly evident in applications that require high real-time and high interactivity, such as multi-session collaborative editing tools, real-time data dashboards, etc. Although JavaScript's state management tool Redux performs well in application state management, it is difficult to manage the real-time and consistency of the state in multi-client synchronization scenarios. On the other hand, Golang, as a back-end server, is often used to build high-performance Web services due to its high concurrent processing capabilities. However, in terms of real-time synchronization and state persistence between the front-end and back-end, existing technical solutions often rely on complex middleware or third-party libraries, which increases development and maintenance costs. Therefore, how to achieve real-time synchronization and persistence of the Golang server and the front-end Redux state in a simple and effective way has become an urgent problem to be solved. Summary of the invention
[0003] In view of this, the present application proposes a WebSocket-based Golang and Redux state real-time synchronization and persistence method to solve the problems reflected in the above background technology.
[0004] According to one aspect of the present application, a method for real-time synchronization and persistence of Golang and Redux states based on WebSocket is provided, comprising the following steps:
[0005] Use Redux for client state management and use WebSocket to communicate with the server;
[0006] Use Golang to implement the WebSocket state manager to manage all WebSocket connections;
[0007] Use Golang to maintain a global state container for real-time state monitoring and updates;
[0008] Use Golang to persist the database state.
[0009] As an optional implementation scheme of the present application, optionally, Redux is used for client state management, and message transmission with the server is implemented through WebSocket, including:
[0010] The client monitors messages from the Golang server in real time via WebSocket;
[0011] Once the status in the client changes, the server is immediately notified via WebSocket to update.
[0012] As an optional implementation scheme of the present application, optionally, Golang is used to implement a WebSocket state manager to manage all WebSocket connections, including:
[0013] Establish a WebSocket connection pool to implement read and write operations on WebSocket connections;
[0014] Process each WebSocket connection to achieve multi-client concurrent management;
[0015] Defines components for processing state reception and synchronization, and is used to monitor and process state change requests from the front end in real time.
[0016] As an optional implementation scheme of the present application, optionally, Golang is used to maintain a global state container for real-time state monitoring and updating, including:
[0017] Implement a global state container based on Golang to store all client states;
[0018] Define the update logic of the state and the actions to be taken when updating.
[0019] As an optional implementation scheme of the present application, optionally, Golang is used to perform state persistence operations on the database, including:
[0020] Define and implement a database interface and operate the database;
[0021] Develop a persistence strategy for incremental state preservation and periodic snapshots.
[0022] As an optional implementation scheme of the present application, optionally, a WebSocket connection pool is established to implement read and write operations on the WebSocket connection, including:
[0023] The connection pool uses sync.Map or map and sync.RWMutex to manage active connections and implement concurrent and safe read and write operations on the connections.
[0024] As an optional implementation scheme of the present application, optionally, it is characterized by defining and implementing a database interface and operating the database, and further comprising:
[0025] The defined and implemented database interface includes methods for saving and loading states;
[0026] Use GORM or database / sql to operate the database.
[0027] According to the second aspect of the present application, a Golang and Redux state real-time synchronization and persistence device based on WebSocket is provided, comprising the following modules:
[0028] The client state management module is used for the client to manage the state through Redux;
[0029] The server-side status management module is used for more concise and easy-to-use status information interaction between the server, client, and database;
[0030] Database module, used to implement database connection management.
[0031] As an optional implementation scheme of the present application, optionally, the server-side state management module includes:
[0032] The WebSocket state manager module manages all WebSocket connections and operations in the server;
[0033] The status monitoring and update module is used to store the status of all clients and the logic and operations of status updates;
[0034] The state persistence module is used to define the interface between the server and the database to implement the operation of persisting the state into the database.
[0035] According to the third aspect of the present application, an electronic device is proposed, comprising:
[0036] processor;
[0037] a memory for storing processor-executable instructions;
[0038] Wherein, the processor is configured to implement a WebSocket-based Golang and Redux state real-time synchronization and persistence method as described in any one of claims 1 to 8 when executing the executable instructions.
[0039] Beneficial effects of this application:
[0040] The present invention realizes two-way communication between the front-end and the back-end through the WebSocket protocol, ensures that state changes can be synchronized in real time, significantly improves user experience and data consistency, has the characteristics of high real-time performance, and utilizes the high concurrent processing capability of Golang and the full-duplex characteristics of WebSocket to avoid the performance overhead and delay problems caused by traditional polling, simplifies the front-end and back-end synchronization logic, and achieves the purpose of simplifying the architecture. The state changes are persistently stored in the database through the back-end service, which ensures the persistence and consistency of the state data, supports the recovery and change of the state, and completes the data persistence. The scheme of the present invention has good scalability, supports real-time synchronization of multiple clients, and is adaptable to complex business needs and application scenarios with large-scale user volumes.
[0041] Other features and aspects of the present application will become apparent from the following detailed description of exemplary embodiments with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate exemplary embodiments, features, and aspects of the present application and, together with the description, serve to explain the principles of the present application.
[0043] Figure 1 A flowchart showing a method for real-time synchronization and persistence of Golang and Redux states based on WebSocket according to an embodiment of the present application is shown;
[0044] Figure 2 A device block diagram showing real-time synchronization and persistence of Golang and Redux states based on WebSocket according to an embodiment of the present application is shown;
[0045] Figure 3 A system architecture diagram showing a method for real-time synchronization and persistence of Golang and Redux states based on WebSocket according to an embodiment of the present application; DETAILED DESCRIPTION
[0046] Various exemplary embodiments, features and aspects of the present application will be described in detail below with reference to the accompanying drawings. The same reference numerals in the accompanying drawings represent elements with the same or similar functions. Although various aspects of the embodiments are shown in the accompanying drawings, the drawings are not necessarily drawn to scale unless otherwise specified.
[0047] Among them, it should be understood that the terms "center", "longitudinal", "lateral", "length", "width", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside", "clockwise", "counterclockwise", "axial", "radial", "circumferential" and the like indicate orientations or positional relationships based on the orientations or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present application or simplifying the description, and do not indicate or imply that the referred device or element must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as a limitation on the present application.
[0048] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this application, the meaning of "plurality" is two or more, unless otherwise clearly and specifically defined.
[0049] The word “exemplary” is used exclusively herein to mean “serving as an example, example, or illustration.” Any embodiment described herein as “exemplary” is not necessarily to be construed as preferred or advantageous over other embodiments.
[0050] In addition, in order to better illustrate the present application, numerous specific details are given in the following specific embodiments. It should be understood by those skilled in the art that the present application can also be implemented without certain specific details. In some examples, methods, means, components and circuits well known to those skilled in the art are not described in detail in order to highlight the subject matter of the present application.
[0051] The architecture design of this application is as follows Figure 3, adopts the collaborative working mode between the front-end, back-end and database, uses the integrated method of WebSocket protocol and Redux state management, realizes the real-time synchronization of the front-end and back-end states through the combination of WebSocket and Redux, and triggers the corresponding Redux operation to update the state. A server-side state manager specifically for the client state is implemented, and the state manager integrates a custom state acceptance and synchronization module, a state monitoring and update module, and a state persistence module, etc. Different modules define a set of quick and easy-to-use interactive systems, which can greatly save development costs. A state persistence mechanism is adopted, that is, a special persistence interface is implemented to persist the state to the database, and different persistence strategies are also supported. This application provides a method for real-time synchronization and persistence of Golang server and JavaScript state manager Redux state based on WebSocket, which solves the pain points of traditional Web applications in state synchronization and persistence. This solution improves the real-time, reliability and scalability of the application by simplifying the front-end and back-end communication logic, and has important application value and promotion prospects.
[0052] Example 1
[0053] Figure 1 The flowchart of the method for real-time synchronization and persistence of Golang and Redux states based on WebSocket according to the first embodiment of the present application is shown. Figure 1 As shown, the flow chart includes:
[0054] S100. Use Redux for client state management and implement message delivery with the server through WebSocket.
[0055] In this embodiment, the client uses Redux for state management. When the state in Redux changes, it will immediately pass a message to the server through WebSocket, and the client also monitors the message from the server in real time through WebSocket. Redux is often used with the React framework for building user interfaces on the front end, but it can also be used in combination with other view libraries or frameworks.
[0056] WebSocket is a protocol for full-duplex communication over a single TCP connection. After the WebSocket connection is established, the client and server can send data to each other, without the need for the client to initiate a request and then wait for the server's response like the HTTP protocol. Therefore, WebSocket is very suitable for real-time synchronization of front-end and back-end status.
[0057] S200. Use Golang to implement the WebSocket state manager to manage all WebSocket connections.
[0058] In this embodiment, a state manager for managing front-end WebSocket connection requests is implemented on the server side based on Golang. Whenever a client sends a WebSocket connection or disconnection request, the WebSocket state manager transmits and processes the request message and creates or deletes the WebSocket connection.
[0059] S300. Use Golang to maintain a global state container for real-time state monitoring and updates.
[0060] In this embodiment, Golang is used to maintain a global state container for storing the states of all clients. In the global state container, methods for state monitoring and state update are defined.
[0061] Used to implement real-time monitoring of the status and update operations when the status changes.
[0062] S400. Use Golang to persist the database state.
[0063] In this embodiment, the client uses Golang to implement a database interface for operating the database. The database interface includes methods for saving and loading states, and the database interface can also define strategy classes according to specific scenario requirements. If the database uses a relational database, GORM (Go's ORM library) can be used to simplify the CRUD operations of the database, or database / sql can be used directly to achieve better performance control and flexibility. In order to persist the state, this method adopts a persistence strategy that combines incremental preservation and periodic snapshots to ensure data reliability and recoverability.
[0064] As an optional implementation scheme of the present application, optionally, Redux is used for client state management, and message transmission with the server is implemented through WebSocket, including: the client monitors messages from the Golang server in real time through WebSocket; once the state in the client changes, the server is immediately notified through WebSocket for update.
[0065] In this embodiment, the client monitors the messages from the Golang server in real time through WebSocket. Once a message is received, the status data in Redux is immediately updated through Redux dispatch. At the same time, when the Redux status changes, the server can also be notified of the status update immediately through Websocket.
[0066] As an optional implementation scheme of the present application, optionally, Golang is used to implement a WebSocket state manager to manage all WebSocket connections, including: establishing a WebSocket connection pool to implement read and write operations on the WebSocket connection; processing each WebSocket connection to achieve concurrent management of multiple clients; defining components for processing state reception and synchronization, for real-time monitoring and processing of front-end state change requests.
[0067] In this embodiment, the connection pool manages active WebSocket connections based on ync.Map or map and sync.RWMutex to achieve concurrent and safe read and write operations. For example, whenever a client connects or disconnects, the connection pool can be added or deleted and broadcast to all connections. When processing the WebSocket connection, it is processed based on Golang's goroutines thread, and channels are used to pass messages between different goroutines. Channels allow data to be safely transferred between multiple goroutines without the need for locks or other synchronization mechanisms, thereby simplifying the complexity of concurrent programming. Therefore, channels are very suitable for broadcasting messages and synchronizing states between connections. The component that processes state reception is used to listen to state change requests from the front end (usually data in JSON format). When the state change request is listened to, the data is parsed and passed to the global state container. The component that processes state synchronization is to use a broadcast channel to broadcast the updated state data to all active WebSocket connections when the state is updated. A broadcast channel is a channel that can broadcast messages to multiple receivers. This can be achieved by traversing all connections in the connection pool and sending new status data on each connection.
[0068] As an optional implementation scheme of the present application, optionally, Golang is used to maintain a global state container for real-time state monitoring and updating, including: implementing a global state container based on Golang to store all client states; defining the state update logic and operations during update.
[0069] In this embodiment, in order to monitor and update the status, the server needs to maintain a global status container. The global status container is implemented based on Golang's sync.Map or map+mutex and is used to store the status of all clients. At the same time, the global status container also defines the status update logic and the operations that need to be performed when a status change request is received. When new status data is passed in, the corresponding status in the global status container is updated. When the status is updated, the status persistence module and the components that perform status synchronization processing in the WebSocket state manager are notified.
[0070] As an optional implementation scheme of the present application, optionally, Golang is used to perform state persistence operations on the database, including: defining and implementing a database interface, and operating the database; formulating a persistence strategy for incremental preservation and periodic snapshots for the state.
[0071] In this embodiment, a PersistenceStrategy interface is defined and implemented to define different persistence strategy classes according to specific needs. The interface includes SaveState and LoadState methods, which are used to save and load states respectively. The Go language ORM library or database / sql can be used to operate the database. This method adopts multiple persistence strategies of incremental preservation and periodic snapshots. The incremental preservation strategy only saves data that has changed since the last save. For example, by comparing the version number of the data or the hash value of the state, the changed part can be identified and only saved. The periodic snapshot strategy is to regularly save the complete state of the entire system or a part as a snapshot. A scheduled task (such as time.Ticker) can be used to regularly trigger snapshot saving and save the current state in full to the database.
[0072] As an optional implementation scheme of the present application, optionally, a WebSocket connection pool is established to implement read and write operations on the WebSocket connection, including: the connection pool uses sync.Map or map and sync.RWMutex to manage active connections to implement concurrent and safe read and write operations on the connection.
[0073] In this embodiment, the connection pool can choose to use sync.Map with sync.RWMutex or map with sync.RWMutex to manage active links according to needs. sync.RWMutex provides a read-write mutex, which is suitable for application scenarios with more reads and less writes.
[0074] sync.Map is a thread-safe map introduced in Go 1.9 that provides concurrent read and write operations without explicit locks. sync.Map manages concurrent access through internal mechanisms, which makes sync.Map with sync.RWMutex more efficient than map with sync.RWMutex in some cases. Using map with sync.RWMutex is a more traditional approach that allows you to control access to the map by explicitly locking and unlocking, with smaller memory overhead and more precise control over when to lock and unlock, which is useful for scenarios that require fine-grained control of concurrent access.
[0075] As an optional implementation scheme of the present application, optionally, it is characterized in that a database interface is defined and implemented, and the database is operated, and it also includes: the defined and implemented database interface includes methods for saving state and loading state; and the database is operated using GORM or database / sql.
[0076] In this embodiment, the database can select a relational database (such as PostgreSQL), etc., and use pgx or database / sql to perform efficient database connection management. Use a connection pool mechanism (such as pgxpool) to optimize the concurrency of database access. The defined and implemented database interface is an interface for implementing a persistence strategy, including SaveState and LoadState methods, where the SaveState method is used to save the state and the LoadState method is used to load the state. This interface also includes a strategy class for defining different strategies according to specific demand scenarios. GORM is a popular object-relational mapping library in the GO language. It provides a high-level API to handle database operations, allowing developers to focus more on business logic rather than database details. Database / sql is part of the Go language standard library and provides a general database interface. This interface is abstract and does not depend on a specific database driver. When operating the database, you can choose one of GORM and database / sql, or you can use them in combination. For example, use database / sql for complex queries or performance-critical operations, and use GORM for daily CRUD operations and model management.
[0077] Example 2
[0078] Based on the same principle as the above method, a real-time synchronization and persistence device for Golang and Redux state based on WebSocket is also proposed. Figure 2, a Golang and Redux state real-time synchronization and persistence device 100 based on WebSocket in an embodiment of the present disclosure includes:
[0079] The client state management module 110 is used for the client to manage the state through Redux;
[0080] The server-side state management module 120 is used for more concise and easy-to-use state information interaction between the server and the client and the database;
[0081] The database module 130 is used to implement connection management of the database.
[0082] As an optional implementation scheme of the present application, optionally, the server-side state management module 120 includes:
[0083] A WebSocket state manager module 121, used to manage all WebSocket connections and operations in the server;
[0084] The state monitoring and updating module 122 is used to store the state of all clients and the logic and operation of state updating;
[0085] The state persistence module 123 is used to define the interface between the server and the database to implement the operation of persisting the state into the database.
[0086] Obviously, those skilled in the art should understand that all or part of the processes in the above-mentioned embodiment method can be implemented by instructing the relevant hardware through a computer program. The program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned control methods. The modules or steps of the present invention can be implemented by a general-purpose computing device. They can be concentrated on a single computing device or distributed on a network composed of multiple computing devices. Optionally, they can be implemented by program codes executable by computing devices, so that they can be stored in a storage device and executed by the computing device, or they can be made into individual integrated circuit modules respectively, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. In this way, the present invention is not limited to any specific combination of hardware and software.
[0087] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment method can be implemented by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned control methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM), a random access memory (RAM), a flash memory (Flash Memory), a hard disk (Hard Disk Drive, abbreviated as: HDD) or a solid-state drive (SSD), etc.; the storage medium can also include a combination of the above-mentioned types of memory.
[0088] Example 3
[0089] Furthermore, an electronic device is proposed, comprising:
[0090] processor;
[0091] a memory for storing processor-executable instructions;
[0092] Among them, the processor is configured to implement the WebSocket-based Golang and Redux state real-time synchronization and persistence method described in Example 1 when executing the executable instructions.
[0093] The electronic device of the disclosed embodiment includes a processor and a memory for storing processor executable instructions. The processor is configured to implement any of the above-mentioned WebSocket-based Golang and Redux state real-time synchronization and persistence methods when executing the executable instructions.
[0094] It should be noted that the number of processors may be one or more. At the same time, the electronic device of the embodiment of the present disclosure may also include an input device and an output device. The processor, memory, input device, and output device may be connected via a bus or in other ways, which are not specifically limited here.
[0095] The memory is a computer-readable storage medium for the WebSocket-based Golang and Redux state real-time synchronization and persistence method, and can be used to store software programs, computer executable programs, and various modules, such as: the program or module corresponding to the WebSocket-based Golang and Redux state real-time synchronization and persistence method of the embodiment of the present disclosure. The processor executes various functional applications and data processing of the electronic device by running the software program or module stored in the memory.
[0096] The input device can be used to receive input numbers or signals. The signal can be a key signal related to user settings and function control of the device / terminal / server. The output device can include a display device such as a display screen.
[0097] The embodiments of the present application have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and changes will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or improvements to the technology in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A method for real-time synchronization and persistence of Golang and Redux states based on WebSocket, characterized in that Use WebSocket to synchronize the front-end and back-end status in real time, including: Use Redux for client state management and use WebSocket to communicate with the server; Use Golang to implement the WebSocket state manager to manage all WebSocket connections; Use Golang to maintain a global state container for real-time state monitoring and updates; Use Golang to persist the database state.
2. The WebSocket-based Golang and Redux state real-time synchronization and persistence method as claimed in claim 1, characterized in that: Use Redux for client state management and implement messaging with the server through WebSocket, including: The client monitors messages from the Golang server in real time via WebSocket; Once the status in the client changes, the server is immediately notified via WebSocket to update.
3. The WebSocket-based Golang and Redux state real-time synchronization and persistence method as claimed in claim 1, characterized in that: Use Golang to implement a WebSocket state manager to manage all WebSocket connections, including: Establish a WebSocket connection pool to implement read and write operations on WebSocket connections; Process each WebSocket connection to achieve multi-client concurrent management; Defines components for processing state reception and synchronization, and is used to monitor and process state change requests from the front end in real time.
4. The WebSocket-based Golang and Redux state real-time synchronization and persistence method as claimed in claim 1, characterized in that: Use Golang to maintain a global state container for real-time state monitoring and updates, including: Implement a global state container based on Golang to store all client states; Define the update logic of the state and the actions to be taken when updating.
5. The WebSocket-based Golang and Redux state real-time synchronization and persistence method described in the claim is characterized in that: Use Golang to persist the database state, including: Define and implement a database interface and operate the database; Develop a persistence strategy for incremental state preservation and periodic snapshots.
6. The WebSocket-based Golang and Redux state real-time synchronization and persistence method as claimed in claim 3, characterized in that: Establish a WebSocket connection pool to implement read and write operations on WebSocket connections, including: The connection pool uses sync.Map or map and sync.RWMutex to manage active connections and implement concurrent and safe read and write operations on the connections.
7. The WebSocket-based Golang and Redux state real-time synchronization and persistence method as claimed in claim 5, characterized in that: Define and implement a database interface and operate the database, including: The defined and implemented database interface includes methods for saving and loading states; Use GORM or database / sql to operate the database.
8. A Golang and Redux state real-time synchronization and persistence device based on WebSocket, characterized in that: include: The client state management module is used for the client to manage the state through Redux; The server-side status management module is used for more concise and easy-to-use status information interaction between the server, client, and database; Database module, used to implement database connection management.
9. The WebSocket-based Golang and Redux state real-time synchronization and persistence device as claimed in claim 8, characterized in that: The server-side state management module includes: The WebSocket state manager module manages all WebSocket connections and operations in the server; The status monitoring and update module is used to store the status of all clients and the logic and operations of status updates; The state persistence module is used to define the interface between the server and the database to implement the operation of persisting the state into the database.
10. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; Wherein, the processor is configured to implement a WebSocket-based Golang and Redux state real-time synchronization and persistence method as described in any one of claims 1 to 8 when executing the executable instructions.