The protocol structure includes 2 parts: Command and Payload.
<command>
Where <command> is a string that indicates the type of command being sent including:
LIST: List all available channelsHOST: Host a new channelMESSAGE: Send a message to the channel
- For
LIST: No payload is needed. - For
HOST: JSON object containingchannel_name,peer_server_ip, andpeer_server_port. - For
MESSAGE: JSON object containingusernameandmessage_content.
Command and payload are separated by the character sequence \r\n.
- List available channels:
LIST\r\n
- Host a new channel:
HOST\r\n{"channel_name": "test_channel", "peer_server_ip": "127.0.0.1", "peer_server_port": 22236}
- Send a message:
MESSAGE\r\n{"username": "user1", "message_content": "Hello, World!"}
All responses follow a consistent format consisting of a status code and a payload.
<status_code>\r\n<response_payload>
OK: The command was processed successfullyREQUEST_ERROR: The client request was invalid (e.g., malformed payload, missing required fields)SERVER_ERROR: An error occurred on the server while processing the request
Response payloads are formatted as JSON objects with contents specific to each command:
Returns an array of available channels with their connection details:
OK\r\n{"channels": [{"channel_name": "test_channel", "peer_server_ip": "127.0.0.1", "peer_server_port": 22236}, {"channel_name": "general", "peer_server_ip": "192.168.1.5", "peer_server_port": 22240}]}
Returns confirmation of channel creation:
OK\r\n{"status": "success", "channel_name": "test_channel"}
Returns confirmation of message delivery:
OK\r\n{"status": "delivered", "timestamp": "2023-11-02T15:04:32Z"}
For invalid client requests:
REQUEST_ERROR\r\n{"error": "Invalid payload", "details": "Missing required field 'channel_name'"}
REQUEST_ERROR\r\n{"error": "Channel not found", "details": "The requested channel 'gaming' does not exist"}
For server-side errors:
SERVER_ERROR\r\n{"error": "Database connection failed", "details": "Unable to store message in channel database"}