signal-fish-server 0.2.0

A lightweight, in-memory WebSocket signaling server for peer-to-peer game networking
Documentation
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# Protocol Reference

Signal Fish Server uses a JSON-based WebSocket protocol. All messages are JSON objects with a `type` field and
optional `data` field.

MessagePack encoding is also supported for game data when `enable_message_pack_game_data` is enabled.

## Client Messages

### Authenticate

Authenticate with app credentials (required when auth is enabled). App ID is a public identifier that identifies
the game application.

```json
{
  "type": "Authenticate",
  "data": {
    "app_id": "my-game"
  }
}

```

Optional fields:

- `sdk_version` - SDK version for debugging and analytics
- `platform` - Platform information (e.g., "unity", "godot", "unreal")
- `game_data_format` - Preferred game data encoding (defaults to JSON text frames)

### JoinRoom

Join or create a room for a specific game. There is no separate room-creation
message. `JoinRoom` behavior depends on `room_code`:

1. Omit `room_code`: create a new room with a generated room code.
2. Provide `room_code` and room exists for that `game_name`: join that room.
3. Provide `room_code` and no room exists for that `game_name`: create a new
   room with that room code.

```json

{
  "type": "JoinRoom",
  "data": {
    "game_name": "my-game",
    "player_name": "Player1"
  }
}

```

Required fields:

- `game_name` - Name of the game
- `player_name` - Name for the player

Optional fields:

- `room_code` - Code used to join/create a room for this `game_name`
- `max_players` - Maximum players (applied only when a new room is created)
- `supports_authority` - Authority support (applied only when a new room is created)
- `relay_transport` - Preferred relay transport protocol (TCP, UDP, or Auto)

### GameData

Send arbitrary game data to other players in the room.

```json

{
  "type": "GameData",
  "data": {
    "data": {
      "action": "move",
      "x": 100,
      "y": 200
    }
  }
}

```

The outer `data` is the serde content tag. The inner `data` is the variant
field and can be any JSON-serializable object.

### PlayerReady

Toggle your own ready state in the lobby. This message has no payload.

Behavior:

1. First send in `lobby` state marks the player ready.
2. Sending again in `lobby` state marks the player unready.
3. The server broadcasts `LobbyStateChanged` after each toggle.
4. When all players are ready, the server sends `GameStarting`.

```json

{
  "type": "PlayerReady"
}

```

This message has no data payload.

If sent while not in a joinable lobby state, the server returns an `Error`
with `INVALID_ROOM_STATE`.

### AuthorityRequest

Request or release game authority.

```json

{
  "type": "AuthorityRequest",
  "data": {
    "become_authority": true
  }
}

```

### LeaveRoom

Leave the current room.

```json

{
  "type": "LeaveRoom"
}

```

### Ping

Heartbeat ping. Server responds with `Pong`.

```json

{
  "type": "Ping"
}

```

### Reconnect

Reconnect to a room after disconnection using authentication token.

```json

{
  "type": "Reconnect",
  "data": {
    "player_id": "player-id",
    "room_id": "room-id",
    "auth_token": "token-string"
  }
}

```

The `auth_token` is generated by the server when a player disconnects and is
provided to the client for reconnection purposes.

### ProvideConnectionInfo

Provide connection info for P2P establishment.

```json

{
  "type": "ProvideConnectionInfo",
  "data": {
    "connection_info": {
      "type": "direct",
      "host": "192.168.1.10",
      "port": 7777
    }
  }
}

```

### JoinAsSpectator

Join a room as a spectator (read-only observer).

```json

{
  "type": "JoinAsSpectator",
  "data": {
    "game_name": "my-game",
    "room_code": "ABC123",
    "spectator_name": "Observer1"
  }
}

```

Required fields:

- `game_name` - Name of the game
- `room_code` - Code of the room to spectate
- `spectator_name` - Name for the spectator

### LeaveSpectator

Leave spectator mode.

```json

{
  "type": "LeaveSpectator"
}

```

This message has no data payload.

## Server Messages

### Authenticated

Authentication successful. Includes app information and rate limits.

```json

{
  "type": "Authenticated",
  "data": {
    "app_name": "my-game",
    "organization": "My Organization",
    "rate_limits": {
      "per_minute": 60,
      "per_hour": 3600,
      "per_day": 86400
    }
  }
}

```

Optional fields:

- `organization` - Organization name (if any)

### ProtocolInfo

SDK/protocol compatibility details advertised after authentication.

```json

{
  "type": "ProtocolInfo",
  "data": {
    "capabilities": ["reconnection", "spectators", "authority"],
    "game_data_formats": ["json", "message_pack"]
  }
}

```

### AuthenticationError

Authentication failed.

```json

{
  "type": "AuthenticationError",
  "data": {
    "error": "Invalid app_id",
    "error_code": "INVALID_APP_ID"
  }
}

```

### RoomJoined

Successfully joined or created a room. This message is sent both when creating
a new room and when joining an existing room. There is no separate
room-created response type.

```json

{
  "type": "RoomJoined",
  "data": {
    "room_id": "uuid-string",
    "room_code": "ABC123",
    "player_id": "your-player-id",
    "game_name": "my-game",
    "max_players": 8,
    "supports_authority": true,
    "current_players": [
      {
        "id": "player-id",
        "name": "Player 1",
        "is_authority": false,
        "is_ready": false,
        "connected_at": "2024-01-01T00:00:00Z"
      }
    ],
    "is_authority": false,
    "lobby_state": "waiting",
    "ready_players": [],
    "relay_type": "WebRTC",
    "current_spectators": []
  }
}

```

### PlayerJoined

Another player joined the room.

```json

{
  "type": "PlayerJoined",
  "data": {
    "player": {
      "id": "player-id",
      "name": "Player 2",
      "is_authority": false,
      "is_ready": false,
      "connected_at": "2024-01-01T00:00:00Z"
    }
  }
}

```

### PlayerLeft

A player left the room.

```json

{
  "type": "PlayerLeft",
  "data": {
    "player_id": "player-id"
  }
}

```

### RoomJoinFailed

Failed to join room.

```json

{
  "type": "RoomJoinFailed",
  "data": {
    "reason": "Room is full",
    "error_code": "ROOM_FULL"
  }
}

```

Note: The `error_code` field is optional.

### RoomLeft

Successfully left room.

```json

{
  "type": "RoomLeft"
}

```

This message has no data payload.

### GameData

Game data relayed from another player.

```json

{
  "type": "GameData",
  "data": {
    "from_player": "player-id",
    "data": {
      "action": "move",
      "x": 100,
      "y": 200
    }
  }
}

```

### GameDataBinary

Binary game data payload from another player. Uses bytes for zero-copy cloning during broadcast.

```json

{
  "type": "GameDataBinary",
  "data": {
    "from_player": "player-id",
    "encoding": "message_pack",
    "payload": "<base64-encoded-bytes>"
  }
}

```

### LobbyStateChanged

Lobby state transitioned.

```json

{
  "type": "LobbyStateChanged",
  "data": {
    "lobby_state": "finalized",
    "ready_players": ["player-id-1", "player-id-2"],
    "all_ready": true
  }
}

```

Possible states:

- `waiting` - Waiting for players to join
- `lobby` - Room is full, players coordinating readiness
- `finalized` - All players ready, game starting

### AuthorityChanged

Authority status changed in the room.

```json

{
  "type": "AuthorityChanged",
  "data": {
    "authority_player": "player-id",
    "you_are_authority": false
  }
}

```

The `authority_player` field can be `null` if no player currently has authority.

### AuthorityResponse

Authority request response.

```json

{
  "type": "AuthorityResponse",
  "data": {
    "granted": true,
    "reason": "Authority granted"
  }
}

```

Note: The `reason` and `error_code` fields are optional.

### GameStarting

Game is starting with peer connection information.

```json

{
  "type": "GameStarting",
  "data": {
    "peer_connections": [
      {
        "player_id": "player-id-1",
        "player_name": "Player 1",
        "is_authority": false,
        "relay_type": "WebRTC"
      }
    ]
  }
}

```

### Error

An error occurred.

```json

{
  "type": "Error",
  "data": {
    "message": "Room is full",
    "error_code": "ROOM_FULL"
  }
}

```

Note: The `error_code` field is optional.

Common error codes:

- `ROOM_FULL` - Room has reached max players
- `ROOM_NOT_FOUND` - Room code does not exist
- `INVALID_GAME_NAME` - Game name validation failed
- `RATE_LIMIT_EXCEEDED` - Too many requests
- `AUTHENTICATION_REQUIRED` - Authentication required
- `INVALID_APP_ID` - Invalid app ID

### Pong

Response to client `Ping`.

```json

{
  "type": "Pong"
}

```

### Reconnected

Reconnection successful. Includes current room state and missed events.

```json

{
  "type": "Reconnected",
  "data": {
    "room_id": "uuid-string",
    "room_code": "ABC123",
    "player_id": "your-player-id",
    "game_name": "my-game",
    "max_players": 8,
    "supports_authority": true,
    "current_players": [
      {
        "id": "player-id",
        "name": "Player 1",
        "is_authority": false,
        "is_ready": false,
        "connected_at": "2024-01-01T00:00:00Z"
      }
    ],
    "is_authority": false,
    "lobby_state": "lobby",
    "ready_players": ["player-id-1"],
    "relay_type": "WebRTC",
    "current_spectators": [],
    "missed_events": [
      {
        "type": "GameData",
        "data": {
          "from_player": "player-id",
          "data": {"action": "move"}
        }
      }
    ]
  }
}

```

### ReconnectionFailed

Reconnection failed.

```json

{
  "type": "ReconnectionFailed",
  "data": {
    "reason": "The reconnection token is invalid or malformed.",
    "error_code": "RECONNECTION_TOKEN_INVALID"
  }
}

```

### PlayerReconnected

Another player reconnected to the room.

```json

{
  "type": "PlayerReconnected",
  "data": {
    "player_id": "player-id"
  }
}

```

### SpectatorJoined

Successfully joined a room as spectator.

```json

{
  "type": "SpectatorJoined",
  "data": {
    "room_id": "uuid-string",
    "room_code": "ABC123",
    "spectator_id": "your-spectator-id",
    "game_name": "my-game",
    "current_players": [
      {
        "id": "player-id",
        "name": "Player 1",
        "is_authority": false,
        "is_ready": false,
        "connected_at": "2024-01-01T00:00:00Z"
      }
    ],
    "current_spectators": [
      {
        "id": "spectator-id",
        "name": "Observer1",
        "connected_at": "2025-01-15T10:35:00Z"
      }
    ],
    "lobby_state": "lobby",
    "reason": "joined"
  }
}

```

Note: The `reason` field is optional.

### SpectatorJoinFailed

Failed to join as spectator.

```json

{
  "type": "SpectatorJoinFailed",
  "data": {
    "reason": "Room not found",
    "error_code": "ROOM_NOT_FOUND"
  }
}

```

Note: The `error_code` field is optional.

### SpectatorLeft

Successfully left spectator mode.

```json

{
  "type": "SpectatorLeft",
  "data": {
    "room_id": "uuid-string",
    "room_code": "ABC123",
    "reason": "voluntary_leave",
    "current_spectators": []
  }
}

```

Note: All fields are optional.

### NewSpectatorJoined

Another spectator joined the room.

```json

{
  "type": "NewSpectatorJoined",
  "data": {
    "spectator": {
      "id": "spectator-id",
      "name": "Observer2",
      "connected_at": "2025-01-15T10:36:00Z"
    },
    "current_spectators": [
      {
        "id": "spectator-id-1",
        "name": "Observer1",
        "connected_at": "2025-01-15T10:35:00Z"
      },
      {
        "id": "spectator-id-2",
        "name": "Observer2",
        "connected_at": "2025-01-15T10:36:00Z"
      }
    ],
    "reason": "joined"
  }
}

```

Note: The `reason` field is optional.

### SpectatorDisconnected

Another spectator left the room.

```json

{
  "type": "SpectatorDisconnected",
  "data": {
    "spectator_id": "spectator-id",
    "reason": "disconnected",
    "current_spectators": []
  }
}

```

Note: The `reason` field is optional.

## Session Flow

```text

Client                              Server
  |                                    |
  |--- Authenticate ------------------>|
  |<-- Authenticated ------------------|
  |                                    |
  |--- JoinRoom (no room_code) ------->|
  |<-- RoomJoined ---------------------|
  |                                    |
  |         (other client joins)       |
  |<-- PlayerJoined -------------------|
  |                                    |
  |--- PlayerReady ------------------->|
  |<-- LobbyStateChanged (lobby) ------|
  |                                    |
  |--- GameData ---------------------->|
  |<-- GameData (from other player) ---|
  |                                    |
  |--- LeaveRoom --------------------->|
  |<-- RoomLeft -----------------------|

```

## Reconnection Flow

When a client disconnects, the server generates a reconnection token bound to
the player's ID and room. The client uses this token along with the `player_id`
and `room_id` (from the original `RoomJoined` response) to reconnect:

```json

{
  "type": "Reconnect",
  "data": {
    "player_id": "your-player-id",
    "room_id": "your-room-id",
    "auth_token": "stored-token"
  }
}

```

On successful reconnection, the server sends a `Reconnected` message with the current room state and any
missed events that occurred during the disconnection.

## Next Steps

- [Getting Started]getting-started.md - Basic usage examples
- [Features]features.md - Complete feature overview