Skip to main content

dox_socketio 3.0.0-alpha.4 copy "dox_socketio: ^3.0.0-alpha.4" to clipboard
dox_socketio: ^3.0.0-alpha.4 copied to clipboard

Socket.IO v4 server (Engine.IO v4, Socket.IO protocol v5) for the Dox framework and plain dart:io.

example/dox_socketio_example.dart

import 'package:dox_socketio/dox_socketio.dart';

/// A service class. In a real app it comes from your DI container.
class MessageService {
  String format(String text) => text.trim();
}

/// Class-based namespace. Its dependencies come in through the constructor.
class ChatNamespace extends NamespaceHandler {
  ChatNamespace(this.messages);

  final MessageService messages;

  @override
  String get path => '/chat';

  @override
  List<Middleware> get middleware => <Middleware>[checkToken];

  void checkToken(Socket socket) {
    if (socket.handshake.auth['token'] != 'secret') {
      throw SocketIoException('unauthorized');
    }
  }

  @override
  void onConnection(Socket socket) {
    socket.join('lobby');
    socket.on('message', onMessage);
    socket.on('disconnect', onDisconnect);
  }

  void onMessage(SocketEvent event) {
    String text = messages.format(event.data.toString());
    event.socket.to('lobby').emit('message', text);
    event.ack('sent');
  }

  void onDisconnect(SocketEvent event) => print('left: ${event.data}');
}

Future<void> main() async {
  SocketIoServer io = SocketIoServer(
    options: ServerOptions(cors: CorsOptions()),
  );
  io.register(ChatNamespace(MessageService()));
  await io.listen(3000);
  print('Socket.IO server on http://127.0.0.1:3000/socket.io/');
}
0
likes
160
points
0
downloads

Documentation

API reference

Publisher

verified publisherdartondox.dev

Weekly Downloads

Socket.IO v4 server (Engine.IO v4, Socket.IO protocol v5) for the Dox framework and plain dart:io.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

ioredis

More

Packages that depend on dox_socketio