class AppChannel extends ApplicationChannel {
List<WebSocket> websockets = [];
// When another isolate gets a websocket message, echo it to
// websockets connected on this isolate.
messageHub.listen(sendBytesToConnectedClients);
Controller get entryPoint {
// Allow websocket clients to connect to ws://host/connect
router.route("/connect").linkFunction((request) async {
var websocket = await WebSocketTransformer.upgrade(request.raw);
websocket.listen(echo, onDone: () {
websockets.remove(websocket);
websockets.add(websocket);
// Take request out of channel
void sendBytesToConnectedClients(List<int> bytes) {
websockets.forEach((ws) {
void echo(List<int> bytes) {
sendBytesToConnectedClients(bytes);
// Send to other isolates