action_cable_stream 0.0.3

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.3

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.

[0.0.3]

  • First release

example/README.md

example #

A new Flutter project.

Getting Started #

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  action_cable_stream: ^0.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:action_cable_stream/action_cable_stream.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Feb 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health issues and suggestions

Document public APIs. (-1 points)

41 out of 41 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
rxdart >=0.20.0 <1.0.0 0.23.1
web_socket_channel ^1.1.0 1.1.0
Transitive dependencies
async 2.4.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
stream_channel 2.0.0
typed_data 1.1.6