Skip to content
pubscout

web_socket

1.0.1, released 2 years ago by dart.dev

An easy-to-use library for communicating with WebSockets that has multiple implementations.

WASM-readyQuiet but busy
$ flutter pub add web_socket
Source repository

What it does

Provides a simple, well-defined WebSocket interface with consistent behavior across different implementations. It includes conformance-tested implementations for browsers, Cupertino, IO, and OkHttp.

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

Use it when

  • You need a consistent WebSocket interface across multiple platforms and implementations.
  • You want an alternative to complex WebSocket packages.

Skip it when

  • You require advanced features exclusive to package:web_socket_channel not present here.

Strengths

  • Consistent behavior across multiple implementations.
  • Passes the same set of conformance tests.

Weaknesses

  • Some implementations like OkHttpWebSocket are currently experimental.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
1
import 'package:web_socket/web_socket.dart';

void main() async {
  final socket =
      await WebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));

  socket.events.listen((e) async {
    switch (e) {
      case TextDataReceived(text: final text):
        print('Received Text: $text');
        await socket.close();
      case BinaryDataReceived(data: final data):
        print('Received Binary: $data');
      case CloseReceived(code: final code, reason: final reason):
        print('Connection to server closed: $code [$reason]');
    }
  });

  socket.sendText('Hello Dart WebSockets! 🎉');
}

Alternatives

Packages that do the same job and could replace web_socket.

Works with

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