action_cable_stream 0.0.2 copy "action_cable_stream: ^0.0.2" to clipboard
action_cable_stream: ^0.0.2 copied to clipboard

outdated

ActionCable client port in dart using streams, available in web, dartVM and flutter.

Pub

ActionCable in Dart using streams #

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.

This library is directly inspired by https://github.com/namiwang/actioncable_dart.

Import #

dependencies:
  action_cable_stream: ^0.0.2

Usage #

  import 'dart:convert';
  import 'package:flutter/material.dart';

  import 'package:action_cable_stream/action_cable_stream.dart';
  import 'package:action_cable_stream/action_cable_stream_states.dart';

  class _MyHomePageState extends State<MyHomePage> {
    final String _channel = 'MyChannel';
    final String _action_cable_url = 'wss://example.com/cable';
    ActionCable _cable;

    @override
    void initState() {
      super.initState();
      _cable = ActionCable.Stream(_action_cable_url);
      _cable.stream.listen((value) {
        if (value is ActionCableConnected) {
          print('ActionCableConnected');
          _cable.subscribeToChannel(_channel, channelParams: {'id': 10});
        } else if (value is ActionCableSubscriptionConfirmed) {
          print('ActionCableSubscriptionConfirmed');
          _cable.performAction(_channel, 'send_message',
              channelParams: {'id': 10}, actionParams: {'body': 'hello world'});
        } else if (value is ActionCableMessage) {
          print('ActionCableMessage ${jsonEncode(value.message)}');
        }
      });
    }

    @override
    void dispose() {
      _cable.disconnect();
    }

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
          appBar: new AppBar(
            title: new Text("Action Cable Stream example"),
          ),
          body: StreamBuilder(
            stream: _cable.stream,
            initialData: ActionCableInitial(),
            builder: (context, AsyncSnapshot<ActionCableDataState> snapshot) {
              return Center(child: buildBody(snapshot));
            },
          ));
    }
  }

  Widget buildBody(AsyncSnapshot<ActionCableDataState> snapshot) {
    final state = snapshot.data;

    if (state is ActionCableInitial ||
        state is ActionCableConnectionLoading ||
        state is ActionCableSubscribeLoading) {
      return Text('Loading...');
    } else if (state is ActionCableError) {
      return Text('Error... ${state.message}');
    } else if (state is ActionCableSubscriptionConfirmed) {
      return Text('Subscription confirmed');
    } else if (state is ActionCableSubscriptionRejected) {
      return Text('Subscription rejected');
    } else if (state is ActionCableMessage) {
      return Text('Message received ${jsonEncode(state.message)}');
    } else if (state is ActionCableDisconnected) {
      return Text('Disconnected');
    } else {
      return Text('Something went wrong');
    }
  }

ActionCable protocol #

Anycable has a great doc on that topic.

5
likes
40
pub points
27%
popularity

Publisher

unverified uploader

ActionCable client port in dart using streams, available in web, dartVM and flutter.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

rxdart, web_socket_channel

More

Packages that depend on action_cable_stream