platform/README.md

132 lines
3.1 KiB
Markdown
Raw Normal View History

2016-04-29 00:11:13 +00:00
# angel_websocket
2017-02-22 22:34:35 +00:00
[![1.0.1](https://img.shields.io/badge/pub-1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket)
2016-12-24 01:45:52 +00:00
[![build status](https://travis-ci.org/angel-dart/websocket.svg)](https://travis-ci.org/angel-dart/websocket)
2016-09-17 20:00:17 +00:00
WebSocket plugin for Angel.
This plugin broadcasts events from hooked services via WebSockets.
2016-12-24 01:45:52 +00:00
In addition, it adds itself to the app's IoC container as `AngelWebSocket`, so that it can be used
2016-09-17 20:00:17 +00:00
in controllers as well.
2016-12-24 01:45:52 +00:00
WebSocket contexts are add to `req.properties` as `'socket'`.
2016-09-03 12:34:01 +00:00
# Usage
**Server-side**
```dart
import "package:angel_framework/angel_framework.dart";
import "package:angel_websocket/server.dart";
main() async {
var app = new Angel();
2017-02-22 22:34:35 +00:00
// Ensure this runs after all our services are in-place
app.justBeforeStart.add(new AngelWebSocket("/ws"));
2016-09-03 12:34:01 +00:00
}
```
2017-01-29 20:02:19 +00:00
Filtering events is easy with services. Just return a `bool`, whether
synchronously or asynchronously.
```dart
myService.properties['ws:filter'] = (WebSocketContext socket) async {
return true;
}
```
2016-09-18 01:35:16 +00:00
**Adding Handlers within a Controller**
2016-09-18 02:53:58 +00:00
`WebSocketController` extends a normal `Controller`, but also listens to WebSockets.
2016-09-18 01:35:16 +00:00
```dart
import 'dart:async';
import "package:angel_framework/angel_framework.dart";
import "package:angel_websocket/server.dart";
@Expose("/")
2016-09-18 02:53:58 +00:00
class MyController extends WebSocketController {
2016-09-18 01:35:16 +00:00
@override
2016-09-18 02:53:58 +00:00
void onConnect(WebSocketContext socket) {
// On connect...
}
// Dependency injection works, too..
@ExposeWs("read_message")
void sendMessage(WebSocketContext socket, Db db) async {
socket.send("found_message", db.collection("messages").findOne(where.id("...")));
2016-09-18 01:35:16 +00:00
}
2017-01-29 20:02:19 +00:00
// Event filtering
@ExposeWs("foo")
void foo() {
broadcast(new WebSocketEvent(...), filter: (socket) async => ...);
}
2016-09-18 01:35:16 +00:00
}
```
2016-09-03 12:34:01 +00:00
**In the Browser**
```dart
import "package:angel_websocket/browser.dart";
main() async {
2016-12-24 01:45:52 +00:00
Angel app = new WebSockets("/ws");
await app.connect();
2016-09-03 12:34:01 +00:00
var Cars = app.service("api/cars");
Cars.onCreated.listen((e) => print("New car: ${e.data}"));
// Happens asynchronously
Cars.create({"brand": "Toyota"});
2016-12-24 01:45:52 +00:00
// Listen for arbitrary events
app.on['custom_event'].listen((event) {
// For example, this might be sent by a
// WebSocketController.
print('Hi!');
});
2016-09-03 12:34:01 +00:00
}
```
**CLI Client**
```dart
import "package:angel_framework/angel_framework" as srv;
2016-12-24 01:45:52 +00:00
import "package:angel_websocket/io.dart";
2016-09-03 12:34:01 +00:00
// You can include these in a shared file and access on both client and server
class Car extends srv.Model {
int year;
String brand, make;
Car({this.year, this.brand, this.make});
@override String toString() => "$year $brand $make";
}
main() async {
2016-12-24 01:45:52 +00:00
Angel app = new WebSockets("/ws");
2016-09-17 20:00:17 +00:00
// Wait for WebSocket connection...
await app.connect();
2016-12-24 01:45:52 +00:00
2016-09-03 12:34:01 +00:00
var Cars = app.service("api/cars", type: Car);
Cars.onCreated.listen((e) {
// Automatically deserialized into a car :)
Car car = e.data;
// 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"});
}
```