phoenix_wings 0.1.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 75

phoenix_wings.dart

A Phoenix Channel implementation for Dart

Attempts to feature match the Javascript implementation found at phoenix.js

Usage

Much of the library is the same whether your code is running in the VM/Flutter or in a browser. Due to differences between the two platforms, the connection setup differs slightly.

Import & Connection (VM/Flutter)

import 'package:phoenix_wings/phoenix_wings.dart';


final socket = new PhoenixSocket("ws://localhost:4000/websocket/socket");

// equivalent to passing connectionProvider: PhoenixIoConnection.provider

Import & Connection (HTML)

import 'package:phoenix_wings/html.dart';


final socket = new PhoenixSocket("ws://localhost:4000/websocket/socket", connectionProvider: PhoenixHtmlConnection.provider);

Common Usage

await socket.connect();

final chatChannel = socket.channel("room:chat", {"id": "myId"});

chatChannel.on("user_entered", PhoenixMessageCallback (Map payload, String _ref, String, _joinRef) {
    print(payload);
});

chatChannel.join();

Testing

Most of the tests are run on the VM. However, the PhoenixHtmlConnection tests must run in a browser. You can use the following commands to run all tests for both platforms:

    # using firefox
    pub run test test/ -p vm,firefox

    # using chrome
    pub run test test/ -p vm,chrome

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:phoenix_wings/phoenix_wings.dart';
import 'package:intl/intl.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Phoenix Wings Chat'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  final socket = PhoenixSocket("ws://localhost:4000/socket/websocket");

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  PhoenixChannel _channel;
  List<ChatMessage> messages = [];
  final TextEditingController _textController = TextEditingController();

  @override
  void initState() {
    connectSocket();
    super.initState();
  }

  connectSocket() async {
    await widget.socket.connect();
    // Create a new PhoenixChannel
    _channel = widget.socket.channel("flutter_chat:lobby");
    // Setup listeners for channel events
    _channel.on("say", _say);

    // Make the request to the server to join the channel
    _channel.join();
  }

  _say(payload, _ref, _joinRef) {
    setState(() {
      messages.insert(0, ChatMessage(text: payload["message"]));
    });
  }

  _sendMessage(message) {
    _channel.push(event: "say", payload: {"message": message});
    _textController.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          Flexible(
            child: ListView.builder(
              reverse: true,
              itemBuilder: (BuildContext context, int index) {
                final message = messages[index];
                return Card(
                    child: Column(
                  children: <Widget>[
                    ListTile(
                        leading: Icon(Icons.message),
                        title: Text(message.text),
                        subtitle: Text(message.time)),
                  ],
                ));
              },
              itemCount: messages.length,
            ),
          ),
          Divider(
            height: 1.0,
          ),
          Container(
              child: MessageComposer(
            textController: _textController,
            sendMessage: _sendMessage,
          ))
        ],
      ),
    );
  }
}

class ChatMessage {
  final String text;
  final DateTime received = DateTime.now();
  ChatMessage({this.text});

  get time => DateFormat.Hms().format(received);
}

class MessageComposer extends StatelessWidget {
  final textController;
  final sendMessage;

  MessageComposer({this.textController, this.sendMessage});
  build(BuildContext context) {
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 8.0),
        child: Row(
          children: <Widget>[
            Flexible(
              child: TextField(
                  controller: textController,
                  onSubmitted: sendMessage,
                  decoration:
                      InputDecoration.collapsed(hintText: "Send a message")),
            ),
            Container(
              child: IconButton(
                  icon: Icon(Icons.send),
                  onPressed: () => sendMessage(textController.text)),
            )
          ],
        ));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  phoenix_wings: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:phoenix_wings/phoenix_wings.dart';
  
Version Uploaded Documentation Archive
0.1.2 May 20, 2018 Go to the documentation of phoenix_wings 0.1.2 Download phoenix_wings 0.1.2 archive
0.0.3 Jan 1, 2018 Go to the documentation of phoenix_wings 0.0.3 Download phoenix_wings 0.0.3 archive
0.0.2 Dec 30, 2017 Go to the documentation of phoenix_wings 0.0.2 Download phoenix_wings 0.0.2 archive
0.0.1 Dec 29, 2017 Go to the documentation of phoenix_wings 0.0.1 Download phoenix_wings 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
59
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8
  • Flutter: 0.5.7

Platforms

Detected platforms: unsure

References Flutter, but has conflicting libraries: package:phoenix_wings/html.dart (components: html, io).

Issues and suggestions

Fix platform conflicts.

References Flutter, but has conflicting libraries: package:phoenix_wings/html.dart (components: html, io).

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Fix lib/src/phoenix_io_connection.dart.

Analysis of lib/src/phoenix_io_connection.dart reported 2 hints:

line 20 col 58: 'OPEN' is deprecated and shouldn't be used.

line 21 col 56: 'CLOSED' is deprecated and shouldn't be used.

Format lib/html.dart.

Run flutter format to format lib/html.dart.

Fix additional 8 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/phoenix_channel.dart (Run flutter format to format lib/src/phoenix_channel.dart.)
  • lib/src/phoenix_connection.dart (Run flutter format to format lib/src/phoenix_connection.dart.)
  • lib/src/phoenix_html_connection.dart (Run flutter format to format lib/src/phoenix_html_connection.dart.)
  • lib/src/phoenix_message.dart (Run flutter format to format lib/src/phoenix_message.dart.)
  • lib/src/phoenix_push.dart (Run flutter format to format lib/src/phoenix_push.dart.)
  • lib/src/phoenix_serializer.dart (Run flutter format to format lib/src/phoenix_serializer.dart.)
  • lib/src/phoenix_socket.dart (Run flutter format to format lib/src/phoenix_socket.dart.)
  • lib/src/phoenix_socket_options.dart (Run flutter format to format lib/src/phoenix_socket_options.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.2.3 <3.0.0
Dev dependencies
dartdoc 0.19.0
flutter
intl ^0.15.6
mockito ^2.2.1
stream_channel ^1.6.4
test 0.12.37