web_socket_channel 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

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 = IOWebSocketChannel.connect("ws://localhost:1234");

  channel.stream.listen((message) {

WebSocketChannel #

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 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.

IOWebSocketChannel #

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 IOWebSocketChannel.connect() should be used.

import 'package:web_socket_channel/io.dart';

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

HtmlWebSocketChannel #

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 HtmlWebSocketChannel.connect() should be used.

import 'package:web_socket_channel/html.dart';

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

1.1.0 #

  • Add WebSocketChannel.connect factory constructor supporting platform independent creation of WebSockets providing the lowest common denominator of support on dart:io and dart:html.

1.0.15 #

  • bug fix don't pass protocols parameter to WebSocket.

1.0.14 #

  • Updates to handle Socket implements Stream<Uint8List>

1.0.13 #

  • Internal changes for consistency with the Dart SDK.

1.0.12 #

  • Allow stream_channel version 2.x

1.0.11 #

  • Fixed description in pubspec.

  • Fixed lints in README.md.

1.0.10 #

  • Fixed links in README.md.

  • Added an example.

  • Fixed analysis lints that affected package score.

1.0.9 #

  • Set max SDK version to <3.0.0.

1.0.8 #

  • Remove use of deprecated constant name.

1.0.7 #

  • Support the latest dev SDK.

1.0.6 #

  • Declare support for async 2.0.0.

1.0.5 #

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

1.0.4 #

  • Support crypto 2.0.0.

1.0.3 #

  • 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.

1.0.2 #

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

1.0.1 #

  • Add support for crypto 1.0.0.

1.0.0 #

  • Initial version


// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

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

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

  channel.stream.listen((message) {

Use this package as a library

1. Depend on it

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

  web_socket_channel: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter 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';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jun 5, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/copy/web_socket_impl.dart. (-4.89 points)

Analysis of lib/src/copy/web_socket_impl.dart reported 10 hints, including:

line 40 col 20: The value of the field 'RESERVED_3' isn't used.

line 41 col 20: The value of the field 'RESERVED_4' isn't used.

line 42 col 20: The value of the field 'RESERVED_5' isn't used.

line 43 col 20: The value of the field 'RESERVED_6' isn't used.

line 44 col 20: The value of the field 'RESERVED_7' isn't used.

Maintenance suggestions

The description is too long. (-2 points)

Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
async >=1.3.0 <3.0.0 2.4.1
crypto >=0.9.2 <3.0.0 2.1.5
stream_channel >=1.2.0 <3.0.0 2.0.0
Transitive dependencies
charcode 1.1.3
collection 1.14.12
convert 2.1.1
typed_data 1.1.6
Dev dependencies
pedantic ^1.0.0
test ^1.2.0