lightstreamer_flutter_client 1.0.2 copy "lightstreamer_flutter_client: ^1.0.2" to clipboard
lightstreamer_flutter_client: ^1.0.2 copied to clipboard

outdated

A Flutter plugin for Lightstreamer.

Lightstreamer Flutter Plugin #

A Flutter plugin for Lightstreamer, built on top of Android Client SDK and of Swift Client SDK.

Target Android Platform #

The Plugin is basically a bridge linking Dart code and native code for Android and iOS platforms. Messages are passed between the Dart application (UI) and host (platform) using platform channels; in particular one MethodChannel and three BasicMessageChannel. The MethodChannel is used to request actions prompted by the UI iteration such as opening and closing the connection with the Lightstreamer server and subscribing and unsubscribing particular Items.

  • The com.lightstreamer.lightstreamer_flutter_client.status BasicMessageChannel is used to send real-time updates to the application about the status of the connection with the Lightstreamer server.
  • The com.lightstreamer.lightstreamer_flutter_client.realtime BasicMessageChannel is used to send real-time updates about the Items the application is subscribed to.
  • The com.lightstreamer.lightstreamer_flutter_client.messages BasicMessageChannel is used to send feedback on the status of send message operation requested by the application.

Getting Started #

Import the package

import 'package:lightstreamer_flutter_client/lightstreamer_flutter_client.dart';

Configure and Start a Lightstreamer Client Session

To connect to a Lightstreamer Server a LightstreamerClient object has to be created, configured, and instructed to connect to a specified endpoint. The platform-specific implementation will take care of this when it receives a 'connect' command on the specific MethodChannel. A minimal version of the code which through the 'connect' command connect to the public Lightstreamer Demo Server (on https://push.lightstreamer.com) will look like this:

    String currentStatus;

    try {
      Map<String, String> params = {"user": "prova1", 
             "password": "qwerty!"};

      currentStatus = await LightstreamerFlutterClient.connect(
              "https://push.lightstreamer.com/", "WELCOME", params) ??
          'Unknown client session status';
    } on PlatformException catch (e) {
      currentStatus =
          "Problems in starting a session with Lightstreamer: '${e.message}' .";
    }

    setState(() {
      _status = currentStatus;
    });

The below code allow the application to listen for any real-time connection state change events:

    LightstreamerFlutterClient.setClientListener(_clientStatus);

    void _clientStatus(String msg) {
    setState(() {
      _status = msg;
    });
  }

Receive Real-Time Updates

In order to receive real-time updates from the Lightstreamer server the client needs to subscribe to specific Items handled by a Data Adapter deployed at the server-side. This can be accomplished by instantiating an object of type Subscription. For more details about Subscription in Lightstreamer see the section 3.2 of the Lightstreamer General Concepts documentation. A sample of code that subscribes three Items of the classic Stock-List example is:

    String static_sub_id = "";

    String? subId = "";
    try {
      Map<String, String> params = {
        "dataAdapter": "STOCKS",
        "requestedMaxFrequency": "7",
        "requestedSnapshot": "yes"
      };
      subId = await LightstreamerFlutterClient.subscribe(
          "MERGE",
          "item2,item7,item8".split(","),
          "last_price,time,stock_name".split(","),
          params);

      static_sub_id = subId as String;

      LightstreamerFlutterClient.setSubscriptionListener(subId, _values);
    } on PlatformException catch (e) {
      // ...
    }

The below code shows an example of implementation of the callback to listen for any real-time updates from your subcriptions:

  void _values(String item, String fieldName, String fieldValue) {
    setState(() {
      _lastUpdate = item + "," + fieldName + "," + fieldValue;
      highlightcolorLast = Colors.yellow;
    });
  }

This code allow to unsubscribing from receiving messages for the previous subscription:

    try {
      await LightstreamerFlutterClient.unsubscribe(static_sub_id);
    } on PlatformException catch (e) {
      // ...
    }

Send Client Messages to the Server

The client can also send messages to the server:

    try {
      await LightstreamerFlutterClient.sendMessageExt(
           "Hello World", "Sequence1", 5000, _clientmessages, true);
    } on PlatformException catch (e) {
      // ...
    }

The below code shows an example of implementation of the callback to listen for send message feedback:

    void _clientmessages(String msg) {
        setState(() {
            _status = msg;
        });
    }

A full running example app is included in the project under example folder.

External Documentations #