Skip to content

Commit 0b194e4

Browse files
authored
fix(realtime_client): make ref field optional in Message class (#1284)
* fix(realtime_client): make ref field optional in Message class Make the ref field nullable (String?) and optional in the Message constructor to match JavaScript SDK behavior. This allows null refs in scenarios like heartbeats and aligns with the JavaScript SDK implementation. - Changed ref from String to String? - Made ref optional in constructor - Updated toJson() to conditionally include ref only when not null - Added comprehensive tests for null ref scenarios Closes SDK-531 * fix: remove unused import
1 parent 3656b4f commit 0b194e4

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

packages/realtime_client/lib/src/message.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ class Message {
55
final String topic;
66
final ChannelEvents event;
77
final dynamic payload;
8-
final String ref;
8+
final String? ref;
99
final String? joinRef;
1010

1111
Message({
1212
required this.topic,
1313
required this.event,
1414
required this.payload,
15-
required this.ref,
15+
this.ref,
1616
this.joinRef,
1717
});
1818

@@ -48,7 +48,7 @@ class Message {
4848
'event':
4949
event != ChannelEvents.heartbeat ? event.eventName() : 'heartbeat',
5050
'payload': processedPayload,
51-
'ref': ref,
51+
if (ref != null) 'ref': ref,
5252
if (joinRef != null) 'join_ref': joinRef,
5353
};
5454
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import 'package:realtime_client/src/constants.dart';
2+
import 'package:realtime_client/src/message.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('Message', () {
7+
test('message with null refs serializes correctly', () {
8+
final message = Message(
9+
topic: 'phoenix',
10+
event: ChannelEvents.heartbeat,
11+
payload: {},
12+
ref: null,
13+
joinRef: null,
14+
);
15+
final json = message.toJson();
16+
expect(json['ref'], isNull);
17+
expect(json['join_ref'], isNull);
18+
expect(json['topic'], equals('phoenix'));
19+
expect(json['event'], equals('heartbeat'));
20+
expect(json['payload'], equals({}));
21+
});
22+
23+
test('heartbeat message with null joinRef', () {
24+
final message = Message(
25+
topic: 'phoenix',
26+
event: ChannelEvents.heartbeat,
27+
payload: {},
28+
ref: '123',
29+
joinRef: null,
30+
);
31+
final json = message.toJson();
32+
expect(json['ref'], equals('123'));
33+
expect(json.containsKey('join_ref'), isFalse);
34+
expect(json['topic'], equals('phoenix'));
35+
expect(json['event'], equals('heartbeat'));
36+
});
37+
38+
test('message with null ref but valid joinRef', () {
39+
final message = Message(
40+
topic: 'room:lobby',
41+
event: ChannelEvents.join,
42+
payload: {'user_id': '123'},
43+
ref: null,
44+
joinRef: 'join-456',
45+
);
46+
final json = message.toJson();
47+
expect(json.containsKey('ref'), isFalse);
48+
expect(json['join_ref'], equals('join-456'));
49+
expect(json['topic'], equals('room:lobby'));
50+
expect(json['payload'], equals({'user_id': '123'}));
51+
});
52+
53+
test('message with both ref and joinRef', () {
54+
final message = Message(
55+
topic: 'room:lobby',
56+
event: ChannelEvents.join,
57+
payload: {'user_id': '123'},
58+
ref: 'ref-789',
59+
joinRef: 'join-456',
60+
);
61+
final json = message.toJson();
62+
expect(json['ref'], equals('ref-789'));
63+
expect(json['join_ref'], equals('join-456'));
64+
expect(json['topic'], equals('room:lobby'));
65+
expect(json['payload'], equals({'user_id': '123'}));
66+
});
67+
68+
test('message with ref but null joinRef', () {
69+
final message = Message(
70+
topic: 'room:lobby',
71+
event: ChannelEvents.leave,
72+
payload: {},
73+
ref: 'ref-999',
74+
joinRef: null,
75+
);
76+
final json = message.toJson();
77+
expect(json['ref'], equals('ref-999'));
78+
expect(json.containsKey('join_ref'), isFalse);
79+
expect(json['topic'], equals('room:lobby'));
80+
});
81+
82+
test('ref parameter is optional in constructor', () {
83+
final message = Message(
84+
topic: 'phoenix',
85+
event: ChannelEvents.heartbeat,
86+
payload: {},
87+
// ref is not provided, should be null
88+
);
89+
expect(message.ref, isNull);
90+
final json = message.toJson();
91+
expect(json.containsKey('ref'), isFalse);
92+
});
93+
});
94+
}

0 commit comments

Comments
 (0)