WebSocket Server Updates: Key Improvements & Changes
What developments have taken place today?
Here’s a summary of the commit descriptions, outlining the changes and their improvements:
WebSocket Server Improvements:
- Message Structure Enhancement (v1): The structure of messages transmitted over the WebSocket server was modified to nest message content within a key-value pair object (“message” key). This improves handling and processing on the receiving end by providing more organized data.
- Connection Management Refactoring (v2): A new
UserConnection
struct was introduced, along with associated functions (AddUserConnection
,RemoveUserConnection
,AddChannelToUser
,RemoveChannelFromUser
,sendErrorToUser
). These changes establish a robust system for managing user connections and channel subscriptions within the server state, ensuring thread safety through mutexes. - WebSocket Command Structure (v3): A comprehensive refactoring of the WebSocket command structure was implemented, introducing constants (
Redis_Chat_Command_*
) and structs (WSCommand
,WSAuth
). New functions (NewWSNewMessage
,NewWSMessageSeen
,NewWSMessageDelivered
) were created to facilitate validation and JSON serialization/deserialization of commands. This significantly improves maintainability and testability, paving the way for a more structured approach to data exchange. - Bot Interaction Simulation (v4): A new test case (
TestTwoBotsChat
) was added, simulating two bots exchanging messages over the WebSocket server. This includes database initialization, bot authentication, channel subscription, message sending/receiving, and verification of routing and delivery. The introduction of helper functionwaitForMessage
further enhances testing capabilities.
Authentication & Flow Simplification (v5):
- Simplified Authentication: Unnecessary error handling code was removed following successful initial authentication attempts, streamlining the authentication flow and improving efficiency.
Testing & Simulation (v6):
- Comprehensive WebSocket Testing: The test function now includes more thorough checks for message handling within a websocket server – subscribing to channels and verifying transmitted/received content.
Utility Script (v7):
- WebSocket Activity Simulator: A bash script was created to simulate WebSocket activity, allowing developers to easily test the server’s behavior with random user actions and message sending. The script uses
websocat
for realistic simulation.
Analytics Removal (v8):
- Analytics Code Deprecation: Several files related to tracking and analytics –
message.ts
,sse.ts
, andanalytics.js
– were removed, indicating a shift away from extensive user behavior tracking.
Build & Dependency Updates (v9):
- Dockerfile Improvements: The Dockerfile was updated to utilize a slimmer Python image and install the
websocket-client
library. Additionally, specific versions of nvm were enforced within the Dockerfile for consistency. A javascript function was added to ensure proper rendering channel functionality.
Minor Updates (v10 & v11):
- .gitignore Update: The .gitignore file was updated to exclude the .gitignore file itself from version control.
- General Code Update: A general code update occurred, encompassing various bug fixes, improvements and updates without specifying particular details.
In essence, these changes represent a significant step forward in improving the WebSocket server’s architecture, functionality, and testability. The refactoring of command handling, coupled with enhanced connection management and bot simulation, lays a solid foundation for future development and ensures reliable message exchange within the system. Removing analytics code indicates a focused approach to core functionality while simplifying the codebase.