Commit c04d5d1
authored
feat(realtime): add support for protocol format 2.0.0 (#1397)
## What
Adds Realtime protocol **2.0.0** support to the Dart/Flutter SDK and
makes the protocol version selectable.
Closes
[SDK-583](https://linear.app/supabase/issue/SDK-583/realtime-add-support-to-protocol-format-200-flutter).
Protocol 2.0.0 changes the WebSocket frame format:
- **Text frames** use the positional JSON array `[join_ref, ref, topic,
event, payload]` instead of the 1.0.0 object layout, reducing JSON work
on the backend (lower latency).
- **Binary frames** allow raw binary payloads for broadcast user events.
## Changes
### Serializer (`serializer.dart`, new)
- Text encode/decode as the positional array `[join_ref, ref, topic,
event, payload]`.
- Binary encode for broadcast pushes (`userBroadcastPush`, kind `3`).
- Binary decode for incoming broadcasts (`userBroadcast`, kind `4`).
- JSON/binary encoding flag and `allowedMetadataKeys`.
- `decode` validates the text frame shape and throws a clear
`FormatException` on malformed input; `onConnMessage` logs and drops
such frames instead of crashing the connection.
### Selectable protocol version
- New `RealtimeProtocolVersion` enum (`v1` → `1.0.0`, `v2` → `2.0.0`);
each carries the `vsn` value sent as the connection parameter.
- `RealtimeClient` takes a `version` parameter, **defaulting to `v2`**.
`v2` uses the serializer; `v1` uses the legacy object-shaped JSON
frames.
### Codec override
- Optional `encode` / `decode` constructor arguments (and the
`RealtimeEncode` / `RealtimeDecode` typedefs) let consumers swap in a
custom serializer; they default to the codec for the selected `version`.
- Return-based signatures: `RealtimeEncode` = `Object
Function(Map<String, dynamic>)`, `RealtimeDecode` = `Map<String,
dynamic> Function(Object)` (synchronous — async/isolate codec is a
follow-up, see below).
### Binary broadcasts
Incoming binary broadcast frames are decoded into the same map shape as
JSON broadcasts. To send binary, provide a `Uint8List` (or any
`TypedData`) under the `payload` key:
\`\`\`dart
channel.sendBroadcastMessage(event: 'file', payload: {'payload':
myUint8List});
\`\`\`
Binary frames are delivered as `Uint8List` on every platform; the web
transport sets `binaryType` to arraybuffer explicitly.
### Misc
- Expanded a few abbreviated identifiers (`obj`/`msg`/`res`) in touched
code.
## Breaking changes
- **Default Realtime protocol is now `2.0.0`** (pass `version:
RealtimeProtocolVersion.v1` to keep the old behavior).
- `RealtimeEncode` / `RealtimeDecode` changed from callback-based to
return-based signatures.
## Related
- The breaking field/method renames (`conn` → `connection`, `connState`
→ `connectionStatus`, `onConnMessage` → `onConnectionMessage`) are split
into #1404, which stacks on this PR and will be retargeted to `v3` after
this merges.
- Follow-up: async codec path for isolate offloading — #1401.
## Tests
- `realtime_client`: 121 passing (new `serializer_test.dart`,
endpointURL `v1`, legacy encode, legacy decode + dispatch,
custom-`encode` override, malformed-frame, binary-broadcast receive).
- `supabase`: 83 passing.
- `supabase_flutter/test/lifecycle_test.dart`: analyze clean.1 parent 1c2e233 commit c04d5d1
10 files changed
Lines changed: 889 additions & 179 deletions
File tree
- packages
- realtime_client
- lib
- src
- websocket
- test
- supabase/test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| |||
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
15 | 27 | | |
16 | 28 | | |
17 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
565 | 565 | | |
566 | 566 | | |
567 | 567 | | |
568 | | - | |
| 568 | + | |
569 | 569 | | |
570 | 570 | | |
571 | 571 | | |
| |||
574 | 574 | | |
575 | 575 | | |
576 | 576 | | |
577 | | - | |
| 577 | + | |
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
581 | | - | |
| 581 | + | |
582 | 582 | | |
583 | | - | |
| 583 | + | |
584 | 584 | | |
585 | 585 | | |
586 | 586 | | |
| |||
592 | 592 | | |
593 | 593 | | |
594 | 594 | | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
595 | 608 | | |
596 | 609 | | |
597 | 610 | | |
| |||
643 | 656 | | |
644 | 657 | | |
645 | 658 | | |
646 | | - | |
| 659 | + | |
647 | 660 | | |
648 | 661 | | |
649 | 662 | | |
650 | 663 | | |
651 | | - | |
| 664 | + | |
652 | 665 | | |
653 | 666 | | |
654 | 667 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
25 | 25 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
| |||
89 | 88 | | |
90 | 89 | | |
91 | 90 | | |
92 | | - | |
93 | 91 | | |
94 | 92 | | |
95 | 93 | | |
96 | 94 | | |
97 | 95 | | |
98 | 96 | | |
| 97 | + | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
117 | 118 | | |
118 | 119 | | |
119 | 120 | | |
| |||
127 | 128 | | |
128 | 129 | | |
129 | 130 | | |
130 | | - | |
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | | - | |
| 147 | + | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
| 150 | + | |
150 | 151 | | |
151 | | - | |
| 152 | + | |
| 153 | + | |
152 | 154 | | |
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
156 | 162 | | |
157 | 163 | | |
158 | 164 | | |
| |||
168 | 174 | | |
169 | 175 | | |
170 | 176 | | |
| 177 | + | |
171 | 178 | | |
172 | 179 | | |
173 | 180 | | |
| |||
178 | 185 | | |
179 | 186 | | |
180 | 187 | | |
181 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
182 | 197 | | |
183 | 198 | | |
184 | 199 | | |
| |||
187 | 202 | | |
188 | 203 | | |
189 | 204 | | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | 205 | | |
197 | 206 | | |
198 | 207 | | |
| |||
244 | 253 | | |
245 | 254 | | |
246 | 255 | | |
247 | | - | |
248 | | - | |
| 256 | + | |
249 | 257 | | |
250 | 258 | | |
251 | 259 | | |
| |||
322 | 330 | | |
323 | 331 | | |
324 | 332 | | |
325 | | - | |
326 | | - | |
327 | | - | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
328 | 339 | | |
329 | 340 | | |
330 | 341 | | |
| |||
394 | 405 | | |
395 | 406 | | |
396 | 407 | | |
397 | | - | |
| 408 | + | |
398 | 409 | | |
399 | 410 | | |
400 | 411 | | |
| |||
408 | 419 | | |
409 | 420 | | |
410 | 421 | | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
420 | 430 | | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
426 | 438 | | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
438 | 456 | | |
439 | 457 | | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
440 | 464 | | |
441 | 465 | | |
442 | 466 | | |
443 | | - | |
| 467 | + | |
444 | 468 | | |
445 | 469 | | |
446 | 470 | | |
| |||
577 | 601 | | |
578 | 602 | | |
579 | 603 | | |
580 | | - | |
| 604 | + | |
581 | 605 | | |
582 | 606 | | |
583 | 607 | | |
| |||
0 commit comments