web_socket_channel
3.0.3, released 2 years ago by tools.dart.dev
Cross‑platform WebSocket channel wrapper for Dart
What it does
Provides a unified WebSocketChannel API that works on Android, iOS, web, and desktop. It wraps platform‑specific WebSocket implementations and exposes a StreamChannel interface with ready, protocol, and close details.
AI-written summary of version 3.0.3, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need a WebSocket connection that works on both mobile and web
- You prefer a Stream‑based API over callbacks
- You want to customize the underlying HttpClient or set a connect timeout
Skip it when
- You only need simple HTTP requests
- You need advanced features like automatic reconnection or message framing
- You are building a server‑side WebSocket handler
Strengths
- Cross‑platform support (mobile, web, desktop)
- Stream‑based API with ready and close details
- Customizable HttpClient and connect timeout
- Supports WebSocket status codes via status.dart
Weaknesses
- No built‑in reconnection logic
- Limited to WebSocket only, no higher‑level protocols
- Requires Dart 3.3+
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 5
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/status.dart' as status;
void main() async {
final wsUrl = Uri.parse('ws://example.com');
final channel = WebSocketChannel.connect(wsUrl);
await channel.ready;
channel.stream.listen((message) {
channel.sink.add('received!');
channel.sink.close(status.goingAway);
});
}Alternatives
Packages that do the same job and could replace web_socket_channel.
- http2
An HTTP/2 implementation in Dart for low-level transport and pooled HTTP/2 requests.
91Compare - sse_channel
Wraps Server‑Sent Events as a StreamChannel for Dart IO and web
56Compare - jikan_api
Dart wrapper for the Tenrai (formerly Jikan) unofficial MyAnimeList API.
66Compare - sync_http
Synchronous HTTP client for Dart using RawSynchronousSockets.
50Compare
Works with
Packages commonly used together with web_socket_channel: ones it builds on, ones built on it, and its own family.
- cryptoweb_socket_channel depends on itProvides SHA, MD5, and HMAC hashing functions for Dart.
- webweb_socket_channel depends on itLightweight bindings for browser APIs via JS interop
- asyncweb_socket_channel depends on itUtility classes and helpers that extend Dart's async library.
- stream_channelweb_socket_channel depends on itAn abstraction for two-way communication channels based on the Dart Stream class.
- web_socketweb_socket_channel depends on itAn easy-to-use library for communicating with WebSockets that has multiple implementations.
- test_apiSame familyThe user facing API for structuring Dart tests and checking expectations.
- shelfSame familyComposable web server middleware for Dart
- sseSame familyBidirectional Server Sent Events client and server for Dart.