ActionCable in Dart
ActionCable is the default realtime websocket framework and protocol in Rails.
This is a dart port of the client and protocol implementation, which is available in web, dartVM and flutter.
Usage
connect
cable = ActionCable.Connect(
'ws://10.0.2.2:3000/cable',
headers: {
'Authorization': 'Some Token',
},
onConnected: (){
print('connected');
},
);
subscribe to channel
cable.subscribeToChannel(
'Chat', // either 'Chat' and 'ChatChannel' is fine
channelParams: { 'id': 1 },
onSubscribed: (){}, // `confirm_subscription` received
onMessage: (Map message) {} // any other message received
);
unsubscribe to channel
cable.unsubscribeToChannel(
'Chat', // either 'Chat' and 'ChatChannel' is fine
);
perform action
cable.performAction(
'Chat', 'send',
channelParams: { 'roomId': 1 },
actionParams: { 'message': 'Hello' }
);
disconnect
cable.disconnect();
ActionCable protocol
Anycable has a great doc on that topic.