stomp_dart_client 0.3.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 92

Stomp Dart #

This library provides an implementation for a STOMP client connecting to a remote server. It should work for both pure dart and flutter.

Usage #

Initialize

The client gets created the specified config, please see the Config section to see all available options

StompClient client = StompClient(
    config: StompConfig(
        url: 'wss://yourserver',
        onConnect: onConnectCallback
    )
);

The connect callback should be used to make sure that we are actually connected before we subscribe or send messages

void onConnectCallback(StompClient client, StompFrame connectFrame) {
    // use the client object passed.
}

Connect

client.activate();

Subscribe

client.subscribe(destination: '/foo/bar', headers: {}, callback: (frame) {
    // Received a frame for this subscription
    print(frame.body);
})

Ack/Nack

client.ack(id: message-id, headers: headers);

client.nack(id: message-id, headers: headers);

Unsubscribe

client.subscribe(...) returns a function which can be called with an optional map of headers

dynamic unsubscribeFn = client.subscribe(destination: '/foo/bar', headers: {}, callback: (frame) {
    // Received a frame for this subscription
    print(frame.body);
})
...
unsubscribeFn(unsubscribeHeaders: {});

Send

client.send(desintation: '/foo/bar', body: 'Your message body', headers: {});

Disconnect

client.deactivate();

StompConfig #

This table shows all available options in StompConfig

OptionDescription
url: StringThe url of the server you want connect to (required)
reconnectDelay: intThe time in milliseconds between reconnect attempts. Set to 0 if you don't want to reconnect automatically
heartbeatOutgoing: intThe time in milliseconds between outgoing heartbeat messages. Set to 0 to not send any heartbeats
heartbeatIncoming: intThe time in milliseconds between incoming heartbeat messages. Set to 0 to not receive any heartbeats
connectionTimeout: DurationTime duration it waits until a connection attempt is aborted
stompConnectHeaders: Map<String, String>The header values which will be used on the STOMP connect frame
webSocketConnectHeaders: Map<String, dynamic>The header values which will be used when connecting to the underyling WebSocket
beforeConnect: FutureA async function which will be awaited before a connection is established.
onConnect: Function(StompClient, StompFrame)Function to be called when the client successfully connected to the server.
onDisconnect: Function(StompFrame)Function to be called when the client disconnects expectedly
onStompError: Function(StompFrame)Function to be called when the stomp server sends an error frame
onUnhandledFrame: Function(StompFrame)Function to be called when the server sends a unrecognized frame
onUnhandledMessage: Function(StompFrame)Function to be called when a subscription message does not have a handler
onUnhandledReceipt: Function(StompFrame)Function to be called when a receipt message does not have a registered watcher
onWebSocketError: Function(dynamic)Function to be called when the underyling WebSocket throws an error
onWebSocketDone: Function()Function to be called when the underyling WebSocket is done/disconnected
onDebugMessage: Function(String)Function to be called for debug messages generated by the internal message handler

Development #

Running unit tests

pub run test -p "chrome,vm" test/

Generating coverage data

pub global activate coverage
pub global run coverage:collect_coverage --port=8111 --out=coverage.json --wait-paused --resume-isolates & dart --disable-service-auth-codes --enable-vm-service=8111 --pause-isolates-on-exit test/test_all.dart

And to convert to lcov

pub global run coverage:format_coverage --lcov --in=coverage.json --out=lcov.info --packages=.packages --report-on=lib

0.3.3 #

  • Properly catch WebSocketException on connect
  • Fixed minor typo in README

0.3.2 #

  • Added Ack/Nack methods (Thanks @justacid). Note: This does not yet work for 1.0 & 1.1
  • (Minor: Reformatted code according to dartanalyze)

0.3.1 #

  • Changed folder structure to please pana.

0.3.0 #

  • Replaced IOWebSocketChannel with WebSocketChannel to be platform agnostic. This means it now also should work for flutter_web.
  • Made tests hybrid tests so that they cover all types of platforms

0.2.3 #

  • Fixed onConnect being called on inactive StompClient

0.2.2 #

  • Reverted type change on stompConnectHeaders because it caused issues on connect

0.2.1 #

  • Fixed a scenario where quick connect/disconnects could cause an exception

0.2.0 #

  • Breaking Change: Renamed connectHeaders to stompConnectHeaders
  • Added webSocketConnectHeaders to StompConfig to be passed to the underyling WebSocket on connection

0.1.6 #

  • Fixed a bug where it would not try to reconnect when the WebSocket connection could not be established
  • Added a connectionTimeout property to the config, to allow control over when a connection attempt is aborted

0.1.5 #

  • More formatting

0.1.4 #

  • Renamed package
  • Added example
  • Incorperated format suggestions

0.1.3 #

  • Removed dependency on non-hosted package to be able to publish the package

example/main.dart

import 'dart:async';
import 'dart:convert';

import 'package:stomp_dart_client/stomp.dart';
import 'package:stomp_dart_client/stomp_config.dart';
import 'package:stomp_dart_client/stomp_frame.dart';

dynamic onConnect(StompClient client, StompFrame frame) {
  client.subscribe(
      destination: '/topic/test/subscription',
      callback: (StompFrame frame) {
        List<dynamic> result = json.decode(frame.body);
        print(result);
      });

  Timer.periodic(Duration(seconds: 10), (_) {
    client.send(
        destination: '/app/test/endpoints', body: json.encode({'a': 123}));
  });
}

final stompClient = StompClient(
    config: StompConfig(
        url: 'ws://localhost:8080',
        onConnect: onConnect,
        onWebSocketError: (dynamic error) => print(error.toString()),
        stompConnectHeaders: {'Authorization': 'Bearer yourToken'},
        webSocketConnectHeaders: {'Authorization': 'Bearer yourToken'}));

void main() {
  stompClient.activate();
}

Use this package as a library

1. Depend on it

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


dependencies:
  stomp_dart_client: ^0.3.3

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:stomp_dart_client/stomp.dart';
import 'package:stomp_dart_client/stomp_config.dart';
import 'package:stomp_dart_client/stomp_frame.dart';
import 'package:stomp_dart_client/stomp_handler.dart';
import 'package:stomp_dart_client/stomp_parser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
84
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

We analyzed this package on May 30, 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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.7 1.1.8
web_socket_channel ^1.1.0 1.1.0
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
typed_data 1.1.6
Dev dependencies
pedantic ^1.0.0
stream_channel ^2.0.0 2.0.0
test ^1.9.2