angel_websocket 1.1.2

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

angel_websocket

Pub build status

WebSocket plugin for Angel.

This plugin broadcasts events from hooked services via WebSockets.

In addition, it adds itself to the app's IoC container as AngelWebSocket, so that it can be used in controllers as well.

WebSocket contexts are add to req.properties as 'socket'.

Usage

Server-side

import "package:angel_framework/angel_framework.dart";
import "package:angel_websocket/server.dart";

main() async {
  var app = new Angel();

  var ws = new AngelWebSocket();
  
  // This is a plug-in. It hooks all your services,
  // to automatically broadcast events.
  await app.configure(ws.configureServer);
  
  // Listen for requests at `/ws`.
  app.all('/ws', ws.handleRequest);
}

Filtering events is easy with hooked services. Just return a bool, whether synchronously or asynchronously.

myService.properties['ws:filter'] = (HookedServiceEvent e, WebSocketContext socket) async {
  return true;
}

myService.index({
  'ws:filter': (e, socket) => ...;
});

Adding Handlers within a Controller

WebSocketController extends a normal Controller, but also listens to WebSockets.

import 'dart:async';
import "package:angel_framework/angel_framework.dart";
import "package:angel_websocket/server.dart";

@Expose("/")
class MyController extends WebSocketController {
  // A reference to the WebSocket plug-in is required.
  MyController(AngelWebSocket ws):super(ws);
  
  @override
  void onConnect(WebSocketContext socket) {
    // On connect...
  }
  
  // Dependency injection works, too..
  @ExposeWs("read_message")
  void sendMessage(WebSocketContext socket, WebSocketAction action, Db db) async {
    socket.send(
      "found_message",
      db.collection("messages").findOne(where.id(action.data['message_id'])));
  }

  // Event filtering
  @ExposeWs("foo")
  void foo() {
    broadcast(new WebSocketEvent(...), filter: (socket) async => ...);
  }
}

Client Use

This repo also provides two client libraries browser and io that extend the base angel_client interface, and allow you to use a very similar API on the client to that of the server.

The provided clients also automatically try to reconnect their WebSockets when disconnected, which means you can restart your development server without having to reload browser windows.

They also provide streams of data that pump out filtered data as it comes in from the server.

Clients can even perform authentication over WebSockets.

In the Browser

import "package:angel_websocket/browser.dart";

main() async {
  Angel app = new WebSockets("/ws");
  await app.connect();

  var Cars = app.service("api/cars");

  Cars.onCreated.listen((car) => print("New car: $car"));

  // Happens asynchronously
  Cars.create({"brand": "Toyota"});

  // Authenticate a WebSocket, if you were not already authenticated...
  app.authenticateViaJwt('<some-jwt>');

  // Listen for arbitrary events
  app.on['custom_event'].listen((event) {
    // For example, this might be sent by a
    // WebSocketController.
    print('Hi!');
  });
}

CLI Client

import "package:angel_framework/common.dart";
import "package:angel_websocket/io.dart";

// You can include these in a shared file and access on both client and server
class Car extends Model {
  int year;
  String brand, make;

  Car({this.year, this.brand, this.make});

  @override String toString() => "$year $brand $make";
}

main() async {
  Angel app = new WebSockets("/ws");

  // Wait for WebSocket connection...
  await app.connect();

  var Cars = app.service("api/cars", type: Car);

  Cars.onCreated.listen((Car car) {
      // Automatically deserialized into a car :)
      //
      // I just bought a new 2016 Toyota Camry!
      print("I just bought a new $car!");
  });

  // Happens asynchronously
  Cars.create({"year": 2016, "brand": "Toyota", "make": "Camry"});

  // Authenticate a WebSocket, if you were not already authenticated...
  app.authenticateViaJwt('<some-jwt>');
}

1.1.2

  • Dart 2 updates.
  • Added handleClient, which is nice for external implementations that plug into AngelWebSocket.

1.1.1

  • Deprecated unwrap.
  • Service streams now pump out e.data, rather than the actual event.

1.1.0+1

  • Added unwrap.

example/main.dart

import "package:angel_framework/angel_framework.dart";
import "package:angel_websocket/server.dart";

main() async {
  var app = new Angel();
  var http = new AngelHttp(app);
  var ws = new AngelWebSocket(app);

  // This is a plug-in. It hooks all your services,
  // to automatically broadcast events.
  await app.configure(ws.configureServer);

  // Listen for requests at `/ws`.
  app.all('/ws', ws.handleRequest);

  var server = await http.startServer('127.0.0.1', 3000);
  print('Listening at http://${server.address.address}:${server.port}');
}

Use this package as a library

1. Depend on it

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


dependencies:
  angel_websocket: ^1.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:angel_websocket/angel_websocket.dart';
  
Version Uploaded Documentation Archive
1.1.2 Jul 10, 2018 Go to the documentation of angel_websocket 1.1.2 Download angel_websocket 1.1.2 archive
1.1.1 Dec 21, 2017 Go to the documentation of angel_websocket 1.1.1 Download angel_websocket 1.1.1 archive
1.1.0+1 Dec 10, 2017 Go to the documentation of angel_websocket 1.1.0+1 Download angel_websocket 1.1.0+1 archive
1.1.0 Dec 7, 2017 Go to the documentation of angel_websocket 1.1.0 Download angel_websocket 1.1.0 archive
1.0.8 Jun 30, 2017 Go to the documentation of angel_websocket 1.0.8 Download angel_websocket 1.0.8 archive
1.0.7 Jun 3, 2017 Go to the documentation of angel_websocket 1.0.7 Download angel_websocket 1.0.7 archive
1.0.6+1 Apr 23, 2017 Go to the documentation of angel_websocket 1.0.6+1 Download angel_websocket 1.0.6+1 archive
1.0.6 Apr 23, 2017 Go to the documentation of angel_websocket 1.0.6 Download angel_websocket 1.0.6 archive
1.0.5 Apr 17, 2017 Go to the documentation of angel_websocket 1.0.5 Download angel_websocket 1.0.5 archive
1.0.4+3 Apr 17, 2017 Go to the documentation of angel_websocket 1.0.4+3 Download angel_websocket 1.0.4+3 archive

All 33 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
73
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.11.8

Platforms

Detected platforms: unsure

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Issues and suggestions

Fix dependencies in pubspec.yaml.

Running pub upgrade failed with the following output:

ERR: The current Dart SDK version is 2.0.0.
 
 Because angel_websocket depends on uuid >=0.0.7 <1.0.3 which requires SDK version <2.0.0, version solving failed.

Fix platform conflicts.

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

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.

Format lib/angel_websocket.dart.

Run dartfmt to format lib/angel_websocket.dart.

Format lib/base_websocket_client.dart.

Run dartfmt to format lib/base_websocket_client.dart.

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/browser.dart (Run dartfmt to format lib/browser.dart.)
  • lib/flutter.dart (Run dartfmt to format lib/flutter.dart.)
  • lib/io.dart (Run dartfmt to format lib/io.dart.)
  • lib/server.dart (Run dartfmt to format lib/server.dart.)
  • lib/websocket_controller.dart (Run dartfmt to format lib/websocket_controller.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0