WebSockets
Introduction
Section titled “Introduction”DiceDB supports WebSocket for connecting to the database, allowing for real-time, bidirectional communication between the client and the server. This can be particularly useful for applications that require low-latency, high-frequency updates.
Connecting to the WebSocket Server
Section titled “Connecting to the WebSocket Server”To connect to the WebSocket server, use the following URL:
ws://your-server-address:port/
Replace your-server-address
and port
with the appropriate values for your server.
Message Format
Section titled “Message Format”WebSocket messages should be sent as plain text, following this format:
COMMAND arg1 arg2 arg3 ...
- The command should be in uppercase.
- Arguments should be separated by spaces.
- For commands that require JSON data, include it as the last argument.
This is very similar to what you’d type in the DiceDB CLI.
Supported Commands
Section titled “Supported Commands”All DiceDB commands are supported over the WebSocket protocol. If some commands are not supported, they will be flagged as such in the command documentation.
For more information on specific commands and their usage, please refer to the command documentation, keeping in mind the WebSocket-specific formatting requirements outlined in this guide.
Special Commands
Section titled “Special Commands”ABORT
: This command will shut down the WebSocket server.
Responses
Section titled “Responses”Responses are sent back through the WebSocket connection as JSON-encoded data. The structure of the response will depend on the command executed.
Example Usage
Section titled “Example Usage”Here’s a simple example of how to interact with the WebSocket server:
const ws = new WebSocket('ws://your-server-address:port/ws');
ws.onopen = function() { console.log('Connected to WebSocket server');
// Set a key ws.send('SET mykey "Hello, WebSocket!"');};
ws.onmessage = function(event) { console.log('Received:', event.data);};
ws.onerror = function(error) { console.error('WebSocket Error:', error);};
ws.onclose = function(event) { console.log('WebSocket connection closed:', event.code, event.reason);};
wscat -c ws://localhost:8379 127.0.0.1:8379> SET foo bar 127.0.0.1:8379< "OK" 127.0.0.1:8379> GET foo 127.0.0.1:8379< "bar"