PlugFox / ws

WS: A compact, highly efficient WebSocket library. Easily establish, manage, and reconnect WebSocket connections in real-time apps.
https://pub.dev/packages/ws
MIT License
39 stars 5 forks source link
cross-platform dart flutter library package pub socket websocket ws

WS: Cross-platform WebSocket client

Pub Actions Status Coverage License: MIT Linter GitHub stars

The ws package provides a cross-platform WebSocket client for both Dart and Flutter applications. It allows you to connect to a WebSocket server, send and receive messages, and handle the connection state changes.


Installation

Add the following dependency to your pubspec.yaml file:

dependencies:
  ws: <version>

Properties

Methods

Options

The WebSocketOptions class provides a configurable set of options for setting up a WebSocket connection on both VM (Virtual Machine) and JS (JavaScript) platforms. This class supports a variety of settings including connection retry strategies, subprotocols, timeouts, message interceptors, and platform-specific configurations.

To use WebSocketOptions, instantiate it directly with the desired configuration for your platform. Use the .vm() factory for VM platform settings and .js() factory for JS platform settings. The .selector() factory can be used to construct options depending on the current platform, allowing for flexible configuration across different environments.

Common Options

Common configuration options applicable across VM and JS platforms:

Option Description
connectionRetryInterval Specifies the Backoff full jitter strategy for reconnecting. Allows tweaks for the reconnect backoff algorithm (min delay, max delay). If not specified, reconnecting is disabled.
protocols Specifies the subprotocols the client is willing to speak.
timeout Specifies the maximum time to wait for the connection to be established. Defaults to 30 seconds if not specified.
afterConnect Specifies the callback function to be called after the connection is established, but before the client is allowed to send user messages.
interceptors Specifies the interceptors for WebSocket messages.

VM Platform Options

Specific to VM (Mobile, Desktop, Server, Console) platform. Not to be used on the web platform.

JS Platform Options

Specific to JS (Browser) platform. Not to be used on the VM platform.

Example

Example of using the ws library to connect to a WebSocket server

Reconnection

The ws package provides a cross-platform WebSocket client that supports automatic reconnection in case of connection loss. The client automatically tries to reconnect to the server when the connection is lost. To handle reconnection-related events, you can register listeners for the stateChanges stream, which notifies you about changes in the connection state. When the connection is closed, the client tries to reconnect with a delay, which increases exponentially with each unsuccessful attempt to prevent overloading the server.

Metrics

The ws package provides a cross-platform WebSocket client that supports metrics. The client automatically collects metrics about the number of sent and received messages, as well as the number of sent and received bytes. To get the metrics, you can use the metrics property, which returns a WebSocketMetrics object. The metrics are updated on demand, so you can get the latest values at any time.

JSON

The ws package provides a cross-platform WebSocket client that supports JSON. The client automatically decodes incoming messages from JSON to Dart objects. To get the decoded messages, you can use the client.stream.json property, which returns a Stream<Map<String, Object?>> of decoded messages.

Features and Roadmap

More resources

Coverage

Changelog

Refer to the Changelog to get all release notes.

Maintainers

Matiunin Mikhail aka Plague Fox

Funding

If you want to support the development of our library, there are several ways you can do it:

We appreciate any form of support, whether it's a financial donation or just a star on GitHub. It helps us to continue developing and improving our library. Thank you for your support!

License

MIT

Tags

web, socket, ws, wss, WebSocket, cross, platform