web_socket_channel 1.0.7

  • README.md
  • Installing
  • Versions
  • 93

The web_socket_channel package provides StreamChannel wrappers for WebSocket connections. It provides a cross-platform WebSocketChannel API, a cross-platform implementation of that API that communicates over an underlying StreamChannel, an implementation that wraps dart:io's WebSocket class, and a similar implementation that wrap's dart:html's.

It also provides constants for the WebSocket protocol's pre-defined status codes in the status.dart library. It's strongly recommended that users import this library should be imported with the prefix status.

import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/status.dart' as status;

main() async {
  var channel = await IOWebSocketChannel.connect("ws://localhost:1234");

  channel.stream.listen((message) {


The WebSocketChannel class's most important role is as the interface for WebSocket stream channels across all implementations and all platforms. In addition to the base StreamChannel interface, it adds a protocol getter that returns the negotiated protocol for the socket, as well as closeCode and closeReason getters that provide information about why the socket closed.

The channel's sink property is also special. It returns a WebSocketSink, which is just like a StreamSink except that its close() method supports optional closeCode and closeReason parameters. These parameters allow the caller to signal to the other socket exactly why they're closing the connection.

WebSocketChannel also works as a cross-platform implementation of the WebSocket protocol. Because it can't initiate or handle HTTP requests in a cross-platform way, the new WebSocketChannel() constructor takes an underlying StreamChannel over which it communicates using the WebSocket protocol. It also provides the static signKey() method to make it easier to implement the initial WebSocket handshake. These are used in the shelf_web_socket package to support WebSockets in a cross-platform way.


The IOWebSocketChannel class wraps dart:io's WebSocket class. Because it imports dart:io, it has its own library, package:web_socket_channel/io.dart. This allows the main WebSocketChannel class to be available on all platforms.

An IOWebSocketChannel can be created by passing a dart:io WebSocket to its constructor. It's more common to want to connect directly to a ws:// or wss:// URL, in which case new IOWebSocketChannel.connect() should be used.

import 'package:web_socket_channel/io.dart';

main() async {
  var channel = new IOWebSocketChannel.connect("ws://localhost:8181");
  channel.sink.listen((message) {
    // ...


The HtmlWebSocketChannel class wraps dart:html's WebSocket class. Because it imports dart:html, it has its own library, package:web_socket_channel/html.dart. This allows the main WebSocketChannel class to be available on all platforms.

An HtmlWebSocketChannel can be created by passing a dart:html WebSocket to its constructor. It's more common to want to connect directly to a ws:// or wss:// URL, in which case new HtmlWebSocketChannel.connect() should be used.

import 'package:web_socket_channel/html.dart';

main() async {
  var channel = new HtmlWebSocketChannel.connect("ws://localhost:8181");
  channel.sink.listen((message) {
    // ...


  • Support the latest dev SDK.


  • Declare support for async 2.0.0.


  • Increase the SDK version constraint to <2.0.0-dev.infinity.


  • Support crypto 2.0.0.


  • Fix all strong-mode errors and warnings.

  • Fix a bug where HtmlWebSocketChannel.close() would crash on non-Dartium browsers if the close code and reason weren't provided explicitly.


  • Properly use BASE64 from dart:convert rather than crypto.


  • Add support for crypto 1.0.0.


  • Initial version

1. Depend on it

Add this to your package's pubspec.yaml file:

  web_socket_channel: "^1.0.7"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:web_socket_channel/web_socket_channel.dart';
Version Uploaded Documentation Archive
1.0.7 Feb 10, 2018 Go to the documentation of web_socket_channel 1.0.7 Download web_socket_channel 1.0.7 archive
1.0.6 Sep 22, 2017 Go to the documentation of web_socket_channel 1.0.6 Download web_socket_channel 1.0.6 archive
1.0.5 Aug 4, 2017 Go to the documentation of web_socket_channel 1.0.5 Download web_socket_channel 1.0.5 archive
1.0.4 Jun 6, 2016 Go to the documentation of web_socket_channel 1.0.4 Download web_socket_channel 1.0.4 archive
1.0.3 May 9, 2016 Go to the documentation of web_socket_channel 1.0.3 Download web_socket_channel 1.0.3 archive
1.0.2 Mar 28, 2016 failed Download web_socket_channel 1.0.2 archive
1.0.1 Mar 23, 2016 failed Download web_socket_channel 1.0.1 archive
1.0.0 Mar 3, 2016 failed Download web_socket_channel 1.0.0 archive
1.0.6-dev Sep 22, 2017 Go to the documentation of web_socket_channel 1.0.6-dev Download web_socket_channel 1.0.6-dev archive


We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6


Describes how popular the package is relative to other packages. [more]
95 / 100
Code health derived from static analysis. [more]
98 / 100
Reflects how tidy and up-to-date the package is. [more]
79 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: unsure


  • Fix platform conflicts.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 error 7 hints.

    Make sure that the imported libraries are not in conflict. Detected components: html, isolate. null

    Strong-mode analysis of lib/src/channel.dart gave the following hint:

    line: 68 col: 20
    'BASE64' is deprecated and shouldn't be used.

    Similar analysis of the following files failed:

    • lib/src/copy/web_socket_impl.dart (hint)
  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use web_socket_channel.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.23.0 <2.0.0
async >=1.3.0 <3.0.0 2.0.6
crypto >=0.9.2 <3.0.0 2.0.2+1
stream_channel ^1.2.0 1.6.5
Transitive dependencies
charcode 1.1.1
collection 1.14.9
convert 2.0.1
path 1.5.1
stack_trace 1.9.2
typed_data 1.1.5
Dev dependencies
test ^0.12.18