Stream Messaging
Stream messaging sends custom bytes over an established TiRTC connection with a specific stream_id. It is useful when the data belongs to a media stream, such as subtitles, overlays, probes, or stream-synchronized state.
Stream messaging is peer-to-peer. After a connection is established, Android, HarmonyOS, iOS, Flutter, and device-side C connection objects can send or receive stream messages.
Agree on stream_id
stream_id identifies one stream inside a connection. The public range is 0 to 15. Audio and video cannot share the same stream_id in one connection, but stream messages can reuse an existing media stream_id or use a separately agreed ID.
The examples below use stream_id = 3.
Send a Stream Message
dart
import 'dart:convert';
import 'dart:typed_data';
final payload = Uint8List.fromList(utf8.encode('probe seq=1'));
final code = conn.sendStreamMessage(
streamId: 3,
timestampMs: DateTime.now().millisecondsSinceEpoch & 0xFFFFFFFF,
data: payload,
);kotlin
val payload = "probe seq=1".encodeToByteArray()
val code = conn.sendStreamMessage(
streamId = 3,
timestampMs = System.currentTimeMillis() and 0xFFFF_FFFFL,
data = payload,
)ts
const payload = new Uint8Array([
0x70, 0x72, 0x6f, 0x62, 0x65, 0x20, 0x73, 0x65, 0x71, 0x3d, 0x31,
]);
const code = conn.sendStreamMessage({
streamId: 3,
timestampMs: Date.now() & 0xffffffff,
data: payload,
});Receive a Stream Message
dart
import 'dart:convert';
import 'dart:typed_data';
conn.onStreamMessage = (int streamId, int timestampMs, Uint8List data) {
debugPrint('stream=$streamId timestampMs=$timestampMs text=${utf8.decode(data)}');
};ts
function asciiString(data: Uint8Array): string {
let result = '';
for (let index = 0; index < data.byteLength; index += 1) {
result += String.fromCharCode(data[index]);
}
return result;
}
conn.onStreamMessage = (streamId: number, timestampMs: number, data: Uint8Array): void => {
console.info(`stream=${streamId} timestampMs=${timestampMs} text=${asciiString(data)}`);
};