Skip to content
pubscout

shelf_web_socket

3.0.0, released 2 years ago by tools.dart.dev

Shelf handler that upgrades HTTP to WebSocket and provides a WebSocketChannel per connection.

Quiet but busy
$ flutter pub add shelf_web_socket
Source repository

What it does

It exposes a single function, webSocketHandler, which takes a callback that receives a WebSocketChannel for each new connection. The channel’s stream and sink are used to receive and send messages over the WebSocket.

AI-written summary of version 3.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • Adding WebSocket support to a Shelf-based server
  • Implementing simple echo or chat services
  • Prototyping WebSocket APIs during development

Skip it when

  • Building a production-grade WebSocket server that requires advanced routing or subprotocol handling
  • Using it in a non-Shelf Dart server

Strengths

  • Easy integration with existing Shelf middleware
  • Type-safe callback signature
  • Supports pingInterval and protocol negotiation
  • No additional dependencies beyond shelf and web_socket_channel

Weaknesses

  • Breaking change in 3.0.0 requires callback signature update
  • Limited to Shelf; not a full-featured WebSocket server framework
  • No built-in authentication or routing for WebSocket paths

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
3
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_web_socket/shelf_web_socket.dart';

void main() {
  var handler = webSocketHandler((webSocket, _) {
    webSocket.stream.listen((message) {
      webSocket.sink.add('echo $message');
    });
  });

  shelf_io.serve(handler, 'localhost', 8080).then((server) {
    print('Serving at ws://${server.address.host}:${server.port}');
  });
}

Alternatives

Packages that do the same job and could replace shelf_web_socket.

  • relic

    Lightweight web server framework inspired by Shelf, with typed APIs and a high‑performance native Zig adapter.

  • shelf_multipart

    Parses multipart and form-data requests for shelf handlers.

  • mcp_server

    Dart implementation of Model Context Protocol (MCP) servers for LLM applications.

  • modbus_client

    Base Modbus client package for sending requests via Serial and TCP/IP.

Works with

Packages commonly used together with shelf_web_socket: ones it builds on, ones built on it, and its own family.