redstone_web_socket is a web socket plugin for Redstone.dart. It uses the shelf_web_socket package to create web socket handlers.

Using @WebSocketHandler with functions

If a function is annotated with @WebSocketHandler, it'll be invoked with a CompatibleWebSocket object for every new established connection:

onConnection(websocket) {
  websocket.listen((message) {
    websocket.add("echo $message");

Using @WebSocketHandler with classes

If a class is annotated with @WebSocketHandler, the plugin will install a event listener for every method annotated with @OnOpen, @OnMessage, @OnError and @OnClose:

class ServerEndPoint {
  void onOpen(WebSocketSession session) {
    print("connection established");

  void onMessage(String message, WebSocketSession session) {
    print("message received: $message");
    session.connection.add("echo $message");

  void onError(error, WebSocketSession session) {
    print("error: $error");

  void onClose(WebSocketSession session) {
    print("connection closed");

Like redstone groups, the class will be instantiated only once, and it can request injectable objects with a constructor (see dependency injection).

Installing handlers

To install web socket handlers, you just have to import redstone_web_socket.dart and call getWebSocketPlugin():

import 'package:redstone/redstone.dart' as app;
import 'package:redstone_web_socket/redstone_web_socket.dart';

void main() {

  //install web socket handlers


Unit tests

This package also provides a simple mock client, which can be used in unit tests:

import 'package:redstone/redstone.dart' as app;
import 'package:redstone_web_socket/redstone_web_socket.dart';
import 'package:unittest/unittest.dart';

main() {

  test("Test echo service", () {

    var completer = new Completer();
    var socket = new MockWebSocket();

    socket.listen((message) {
      expect(message, equals("echo message"));


    openMockConnection("/ws", socket);


    return completer.future;