summaryrefslogtreecommitdiff
path: root/websocket-protocol.md
blob: 6e8734a166a9c1f389f6d9de231cb5622922968b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# WebSocket Protocol

This document describes the WebSocket events used for real-time collaborative graph editing.

## Connection

- **Endpoint:** `/ws`
- **Protocol:** WebSocket (ws:// or wss://)

On connection, the server immediately sends a `fullSync` event with the current graph state.

---

## Event Types

### Client Request Events

These events are sent from the client to the server.

#### `addNode`

Request to add a new node to the graph.

```json
{
  "type": "addNode",
  "node": {
    "x": 1.5,
    "y": 2.0,
    "z": -1.0
  }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `node.x` | float | X coordinate |
| `node.y` | float | Y coordinate |
| `node.z` | float | Z coordinate |

**Server behavior:** Assigns a unique ID to the node, adds it to the graph, and broadcasts the confirmed node to all clients.

---

#### `addEdge`

Request to add an edge between two existing nodes.

```json
{
  "type": "addEdge",
  "edge": {
    "from": 0,
    "to": 3
  }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `edge.from` | int | Source node ID |
| `edge.to` | int | Target node ID |

**Server behavior:** Validates that both nodes exist, the edge doesn't already exist, and nodes aren't the same. If valid, adds the edge and broadcasts to all clients. Invalid requests are silently ignored.

---

#### `reset`

Request to clear the graph and restore initial state.

```json
{
  "type": "reset"
}
```

**Server behavior:** Clears all nodes and edges, then broadcasts a `fullSync` with an empty graph to all clients.

---

### Server Publish Events

These events are sent from the server to clients.

#### `fullSync`

Full graph state synchronization. Sent on initial connection and after reset.

```json
{
  "type": "fullSync",
  "graph": {
    "nodes": [
      {"id": 0, "x": 0, "y": 0, "z": 0},
      {"id": 1, "x": 3, "y": 2, "z": 1}
    ],
    "edges": [
      {"from": 0, "to": 1}
    ]
  }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `graph.nodes` | array | All nodes in the graph |
| `graph.nodes[].id` | int | Unique node identifier |
| `graph.nodes[].x` | float | X coordinate |
| `graph.nodes[].y` | float | Y coordinate |
| `graph.nodes[].z` | float | Z coordinate |
| `graph.edges` | array | All edges in the graph |
| `graph.edges[].from` | int | Source node ID |
| `graph.edges[].to` | int | Target node ID |

**Client behavior:** Replace entire local graph state with server state. Rebuild visualization.

---

#### `addNode`

Broadcast when a node has been added (by any client).

```json
{
  "type": "addNode",
  "node": {
    "id": 6,
    "x": 1.5,
    "y": 2.0,
    "z": -1.0
  }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `node.id` | int | Server-assigned unique ID |
| `node.x` | float | X coordinate |
| `node.y` | float | Y coordinate |
| `node.z` | float | Z coordinate |

**Client behavior:** Add the node to local graph and create visual representation.

---

#### `addEdge`

Broadcast when an edge has been added (by any client).

```json
{
  "type": "addEdge",
  "edge": {
    "from": 0,
    "to": 6
  }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `edge.from` | int | Source node ID |
| `edge.to` | int | Target node ID |

**Client behavior:** Add the edge to local graph and create visual representation.

---

## Message Flow

```
Client A                    Server                    Client B
    |                          |                          |
    |-------- connect -------->|                          |
    |<------ fullSync ---------|                          |
    |                          |                          |
    |-------- addNode -------->|                          |
    |                          |--- (validate & assign ID)
    |<------ addNode ----------|-------- addNode -------->|
    |                          |                          |
    |                          |<------- addEdge ---------|
    |                          |--- (validate)            |
    |<------ addEdge ----------|-------- addEdge -------->|
    |                          |                          |
```

## Error Handling

- Invalid operations (bad node IDs, duplicate edges, self-loops) are silently ignored
- The server never sends error messages; invalid requests simply produce no broadcast
- Clients should not assume their request succeeded until they receive the broadcast