subscribe method
ActionChannel
subscribe(
- String channelName, {
- Map? channelParams,
- VoidCallback? onSubscribed,
- VoidCallback? onDisconnected,
- List<
ActionCallback> callbacks = const [],
Subscribe to a channel
void receiveMessage(Map payload) => print(payload);
final actionCallback = ActionCallback(name: 'receive_message', callback: receiveMessage);
ActionChannel channel = cable.subscribe(
'Chat', // either 'Chat' and 'ChatChannel' is fine
channelParams: { 'room': 'private' },
onSubscribed: (){}, // `confirm_subscription` received
onDisconnected: (){}, // `disconnect` received
callbacks: [actionCallback] // Callback list to able the server to call any method that you registered in your aplicaticon
);
Implementation
ActionChannel subscribe(
String channelName, {
Map? channelParams,
VoidCallback? onSubscribed,
VoidCallback? onDisconnected,
List<ActionCallback> callbacks = const [],
}) {
final identifier = IdentifierHelper.encodeChanelId(
channelName,
channelParams,
);
CallbacksStore.subscribed[identifier] = onSubscribed;
CallbacksStore.diconnected[identifier] = onDisconnected;
CallbacksStore.message[identifier] = callbacks;
_send({'identifier': identifier, 'command': 'subscribe'});
return ActionChannel(identifier: identifier, sendMessageCallback: _send);
}