web_socket_support 0.2.3 icon indicating copy to clipboard operation
web_socket_support: ^0.2.3 copied to clipboard

PlatformAndroid

Flutter plugin for utilising OkHttp websocket implementation on Android. Supports text and binary messages. Works even when app is in background or when screen is locked.

Websocket support #

pub package web_socket_support codecov

A Flutter plugin for websockets on Android (currently). This plugin is based on okHttp (for Android platform).

Plugin was created as an attempt to overcome shortcomings of Flutter standard WebSocket implementation (cookbook) like connection not staying open while screen is locked or the application is in background. This plugin solves these problems.

Introduction #

Websocket support uses Platform Channel to expose Dart APIs that Flutter application can use to communicate with platform specific websocket native libraries. For andorid, chosen java Websocket implementation is OkHttp.

Example #

// WebSocketConnection will be obtained via _onWsOpen callback in WebSocketClient
WebSocketConnection _webSocketConnection;

// instantiate WebSocketClient with DefaultWebSocketListener and some callbacks
// Of course you can use you own WebSocketListener implementation
final WebSocketClient _wsClient = WebSocketClient(DefaultWebSocketListener.forTextMessages(
        (wsc) => _webSocketConnection = wsc,                       // _onWsOpen callback
        (code, msg) => print('Connection closed. Resaon: $msg'),  // _onWsClosed callback
        (msg) => print('Message received: $msg')));               // _onStringMessage callback
// ...
// connect to remote ws endpoint
await _wsClient.connect("ws://echo.websocket.org");

// ...
// After connection is established, use obtained WebSocketConnection instance to send messages
_webSocketConnection.sendTextMessage('Hello from Websocket support');

or see /example/lib/main.dart

Build Dependencies #

  • Dart SDK version: >=2.20.0 <3.0.0
  • Flutter SDK version: >=1.16.0
  • Java version: 11
  • AGP version: 7.2.x
  • Gradle version: 7.4.2
  • Android SDK min version: 21
  • Android SDK target version: 32

TODO #

Unfortunately, iOS implementation is still missing. So, if you have know-how, and you're willing to implement it - you will be more than welcomed. Preffered WebSocket libs are NWWebSocket and Starscream, but we are opened for other options as well.

Contributing #

See the Contributing guide for details on contributing to this project.

4
likes
130
pub points
31%
popularity

Publisher

verified publisher iconsharpbitstudio.tech

Flutter plugin for utilising OkHttp websocket implementation on Android. Supports text and binary messages. Works even when app is in background or when screen is locked.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.BSD-3-Clause (LICENSE)

Dependencies

async, flutter, meta, plugin_platform_interface, web_socket_support_platform_interface

More

Packages that depend on web_socket_support