subscribe method

ActionChannel subscribe(
  1. String channelName, {
  2. Map? channelParams,
  3. VoidCallback? onSubscribed,
  4. VoidCallback? onDisconnected,
  5. 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);
}