websocket_manager 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

Websocket Manager #

A Flutter plugin for Android and iOS supports websockets. This plugin is based on two different native libraries Starscream for iOS and okHttp for Android.

This plugin was created due to our necessity to maintain a WebSocket connection active in background while Flutter's WebSocket from cookbook doesn't keep alive while screen is locked or the application was in background.

Introduction #

Websocket Manager doesn't manipulate websockets in Dart codes directly, instead, the plugin uses Platform Channel to expose Dart APIs that Flutter application can use to communicate with two very powerful websocket native libraries. Because of that, all credits belong to these libraries.

How to install #

Android #

You only need this configuration if your server doesn't have SSL/TLS

Since Android P http is blocked by default and there are many ways to configure. One way to configure is explicitly saying that you accept clear text for some host.

  • Create res/xml/network_security_config.xml with content:
<?xml version="1.0" encoding="utf-8"?>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your_domain</domain>
  • Point to this file from your manifest (for bonus points add it only for the test manifest):
    <activity android:name=" (...)

iOS #

Doesn't require any configuration

Example #

int messageNum = 0;
// Configure WebSocket url
final socket = WebsocketManager('wss://echo.websocket.org');
// Listen to close message
socket.onClose((dynamic message) {
// Listen to server messages
socket.onMessage((dynamic message) {
    print('recv: $message');
    if messageNum == 10 {
    } else {
        messageNum += 1;
        final String msg = '$messageNum: ${DateTime.now()}';
        print('send: $msg');
// Connect to server

Credits #

0.2.0 [2019-12-12]

  • Updates okHttp plugin.
  • Fix bug on a Android failure event that wasn't treated correct.

0.1.3 [2019-12-12]

  • Fix bug where the close event channel is closed before sending close event.

0.1.2 [2019-12-12]

  • Calls flutter when there are no listeners to an event channel in order to keep a listener.

0.1.1 [2019-12-12]

  • Fix bug on Android where disconnect always reconnects due to retry.

0.1.0 [2019-12-09]

  • Fix bug on listen to event channel.
  • Fix bug on iOS where disconnect always reconnects due to retry.

0.0.2 [2019-12-03]

  • Adds comments, analysis file and applies health suggestions.

0.0.1 [2019-12-02]

  • Initial release.


websocket_manager_example #

Demonstrates how to use the websocket_manager plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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

  websocket_manager: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support 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:websocket_manager/websocket_manager.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 Jan 16, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

Fix lib/websocket_manager.dart. (-1 points)

Analysis of lib/websocket_manager.dart reported 2 hints:

line 31 col 38: The value of the field '_onCloseSubscription' isn't used.

line 31 col 38: Cancel instances of dart.async.StreamSubscription.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies