The primary communication channel for the chat is WebSockets between the ChatServer and ChatClient (the front-end web page).
The protocol was made up as it went and here is some (hopefully current) documentation of what the different message types and contents look like.
Messages are delivered as JSON objects in both directions.
# WebRTC Workflow
WebRTC only kicks in when a user wants to see the webcam stream shared by a broadcaster. Simply turning on your webcam doesn't start any WebRTC stuff - it's when somebody clicks to see your cam, or you click to see somebody else's.
Since the WebRTC workflow is always triggered by _somebody_ clicking on the video icon to open a broadcaster's camera we will start there.
The one who initiates the connection is called the **offerer** (they send the first offer to connect) and the one sharing video is the **answerer** in WebRTC parlance.
1. The offerer clicks the video button to begin the process. This sends an [open](#open) message to the server.
2. The server echoes the [open](#open) back to the offerer and sends a [ring](#ring) to the answerer, to let them know that the offerer wants to connect.
* For the answerer, the `ring` message triggers the "has opened your camera" notice in chat.
3. Both the offerer and answerer will use the server to negotiate a WebRTC peer connection.
* WebRTC is a built-in browser standard and the two browsers will negotiate "ICE candidates" and "session description protocol" (SDP) messages.
* The [candidate](#candidate) and [sdp](#sdp) actions on the chat server allow simple relaying of these messages between browsers.
* The answerer adds their video stream to the RTC PeerConnection so that once they are established, the offerer receives the video.
4. When connectivity is established, the offerer sends a [watch](#watch) message which the server passes to the answerer, so that their username apprars in the Watching list.
The video stream can be interrupted and closed via various methods:
* When the answerer turns off their camera, they close all RTC PeerConnections with the offerers.
* When the PeerConnection is closed, the offerer deletes the `<video>` widget from the page (turning off the camera feed).
* Also, a `who` update that says a person's videoActive went false will instruct all clients who had the video open, to close it (in case the PeerConnection closure didn't already do this).
* If a user exits the room, e.g. exited their browser abruptly without gracefully closing PeerConnections, any client who had their video open will close it immediately.
If the message is a DM, the channel will be the username prepended by an @ symbol and the ChatClient will add it to the appropriate DM thread (creating a new DM thread if needed).
The server will massage and validate the image data and then send it to others in the chat via a normal `message` containing an `<img>` tag with a data: URL - directly passing the image data to other chatters without needing to store it somewhere with a public URL.
The `presence` message is just like a `message` but is designed for join/exit chat events.
```javascript
// Server message
{
"action": "presence",
"username": "soandso",
"message": "has joined the room!"
}
```
## Me
Sent by: Client, Server.
The "me" action communicates the user's current state and settings to the server. It will usually also trigger a "who" action to refresh the Who List for all chatters.
The client sends "me" messages to send their webcam broadcast status and NSFW flag:
This command is sent when a viewer wants to **open** the webcam of a broadcaster and see their video.
```javascript
// Client Open
{
"action": "open",
"username": "target"
}
```
The server echos the `open` command back at the person who initiated it:
```javascript
// Server Open
{
"action": "open",
"username": "target",
"openSecret": "random string (not actually used)"
}
```
And for the one sharing their webcam, sends a `ring` message.
## Ring
Sent by: Server.
This is sent to the user who is sharing their webcam, to notify them that a viewer wants to connect.
```javascript
// Server Ring
{
"action": "ring",
"username": "viewer",
"openSecret": "random string (not actually used)"
}
```
The user will then initiate a WebRTC peer-to-peer connection with the viewer to share their video to them.
## Watch, Unwatch
Sent by: Client, Server.
When a viewing client successfully receives video frames from the sender, they send a `watch` command to update the sender's Watching list, and will send an `unwatch` command when they close the video.
The server passes the watch/unwatch message to the broadcaster.
```javascript
{
"action": "watch",
"username": "viewer"
}
```
## Mute, Unmute
Sent by: Client.
The mute command tells the server that you are muting the user.
```javascript
// Client Mute
{
"action": "mute",
"username": "target"
}
```
When the user is muted:
* The server will lie about your camera status on `who` messages to that user, always showing your camera as not active.
* If they were already watching your camera, they see that you have turned your camera off and they disconnect.
* The server will not send you any `message` from that user.
The `unmute` action does the opposite and removes the mute status:
The blocklist command is basically a bulk mute for (potentially) many usernames at once.
```javascript
// Client blocklist
{
"action": "blocklist",
"usernames": [ "target1", "target2", "target3" ]
}
```
How this works: if you have an existing website and use JWT authentication to sign users into chat, your site can pre-emptively sync the user's block list **before** the user enters the room, using the `/api/blocklist` endpoint (see the README.md for BareRTC).
The chat server holds onto blocklists temporarily in memory: when that user loads the chat room (with a JWT token!), the front-end page receives the cached blocklist. As part of the "on connected" handler, the chat page sends the `blocklist` command over WebSocket to perform a mass mute on these users in one go.
The reason for this workflow is in case the chat server is rebooted _while_ the user is in the room. The cached blocklist pushed by your website is forgotten by the chat server back-end, but the client's page was still open with the cached blocklist already, and it will send the `blocklist` command to the server when it reconnects, eliminating any gaps.
This command is to kick a viewer off of your webcam and block them from opening your webcam again.
```javascript
// Client Boot
{
"action": "boot",
"username": "target"
}
```
When a user is booted:
* They are kicked off your camera.
* The chat server lies to them about your camera status on future `who` messages - showing that your camera is not running.
Note: it is designed that the person being booted off can not detect that they have been booted. They will see your RTC PeerConnection close + get a Who List that says you are not sharing video - exactly the same as if you had simply turned off your camera completely.
## WebRTC Signaling
Sent by: Client, Server.
The `candidate` and `sdp` actions are used as part of WebRTC signaling negotiations where the two browsers (the broadcaster and viewer) try and connect to share video.