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.
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.
91Compare - shelf_multipart
Parses multipart and form-data requests for shelf handlers.
83Compare - mcp_server
Dart implementation of Model Context Protocol (MCP) servers for LLM applications.
72Compare - modbus_client
Base Modbus client package for sending requests via Serial and TCP/IP.
56Compare
Works with
Packages commonly used together with shelf_web_socket: ones it builds on, ones built on it, and its own family.
- shelfshelf_web_socket depends on itComposable web server middleware for Dart
- web_socket_channelshelf_web_socket depends on itCross‑platform WebSocket channel wrapper for Dart
- stream_channelshelf_web_socket depends on itAn abstraction for two-way communication channels based on the Dart Stream class.
- test_apiSame familyThe user facing API for structuring Dart tests and checking expectations.
- shelf_staticSame familyServe static files over HTTP with the shelf package
- shelf_packages_handlerSame familyShelf handler that serves a `packages/` directory of Dart package assets.
- shelf_proxySame familyA shelf handler for proxying HTTP requests to another server.
- sseSame familyBidirectional Server Sent Events client and server for Dart.