RealtimeMessage class
Wire message envelope for Bloom Realtime channel, broadcast, and presence communication.
Encapsulates message framing across WebSockets or server-sent events. Pure Dart,
requiring only dart:convert, ensuring compatibility across Dart VM, SSR, and browser runtimes.
Message Types
- Channel subscription: typeSubscribe, typeUnsubscribe
- Broadcast events: typeBroadcast
- Presence tracking: typePresenceJoin, typePresenceLeave, typePresenceState
- System heartbeats: typePing, typePong, typeError
Example
// Construct a broadcast message
final msg = RealtimeMessage.broadcast('chat:general', {'text': 'Hello, world!'});
final jsonString = msg.encode();
// Parse incoming wire message
final parsed = RealtimeMessage.tryParse(jsonString);
if (parsed != null && parsed.type == RealtimeMessage.typeBroadcast) {
print('Message on ${parsed.channel}: ${parsed.payload}');
}
Constructors
-
RealtimeMessage({required String type, String? channel, Map<
String, dynamic> payload = const {}}) -
Creates a RealtimeMessage envelope with a
type, optionalchannel, and optionalpayload.const -
RealtimeMessage.broadcast(String channel, Map<
String, dynamic> payload) -
Creates a broadcast event message on
channelwith the givenpayload.factory -
RealtimeMessage.fromJson(Map<
String, dynamic> json) -
Deserializes a RealtimeMessage from a decoded JSON map.
factory
-
RealtimeMessage.presenceJoin(String channel, Map<
String, dynamic> userInfo) -
Creates a presence join message announcing
userInfometadata inchannel.factory -
RealtimeMessage.presenceLeave(String channel, Map<
String, dynamic> userInfo) -
Creates a presence leave message announcing that a user left
channel.factory -
RealtimeMessage.presenceState(String channel, List<
Map< presences)String, dynamic> > -
Creates a presence state snapshot message containing all active
presencesinchannel.factory - RealtimeMessage.subscribe(String channel)
-
Creates a subscription message targeting
channel.factory - RealtimeMessage.unsubscribe(String channel)
-
Creates an unsubscription message targeting
channel.factory
Properties
- channel → String?
-
Channel identifier associated with this message, or
nullfor system-level messages like ping/pong.final - hashCode → int
-
The hash code for this object.
no setterinherited
-
payload
→ Map<
String, dynamic> -
Payload map associated with the event.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- type → String
-
Message type identifier (e.g.
'subscribe','broadcast','presence_join').final
Methods
-
encode(
) → String - Encodes this message to a JSON string.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, dynamic> - Serializes this message to a JSON-compatible map.
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
tryParse(
dynamic raw) → RealtimeMessage? -
Attempts to parse
raw(a JSON string, UTF-8 byte list, or Map) into a RealtimeMessage.
Constants
- typeBroadcast → const String
- Wire action type indicating an arbitrary event broadcast to channel subscribers.
- typeError → const String
- Wire action type indicating a server-side or protocol error.
- typePing → const String
- Wire heartbeat ping message.
- typePong → const String
- Wire heartbeat pong response.
- typePresenceJoin → const String
- Wire action type indicating a client joined channel presence.
- typePresenceLeave → const String
- Wire action type indicating a client left channel presence.
- typePresenceState → const String
- Wire action type carrying the full snapshot of active channel presences.
- typeSubscribe → const String
- Wire action type indicating a channel subscription request.
- typeUnsubscribe → const String
- Wire action type indicating an unsubscription request.