signalr_client_mrj 0.1.7-dev.11

  • Readme
  • Changelog
  • Installing
  • 67

signalr_client #

pub package

A Flutter SignalR Client for ASP.NET Core.
ASP.NET Core SignalR is an open-source library that simplifies adding real-time web functionality to apps. Real-time web functionality enables server-side code to push content to clients instantly.

The client is able to invoke server side hub functions (including streaming functions) and to receive method invocations issued by the server.

The client supports the following transport protocols:

  • WebSocket
  • Service Side Events
  • Long Polling

The client supports the following hub protocols:

  • Json
  • MessagePack - Since I can't find a MessagePack library that support the current flutter version.

Examples #

Getting Started #

Add signalr_client to your pubspec.yaml dependencies:

...
dependencies:
  flutter:
    sdk: flutter

  signalr_client:
...

Usage #

Let's demo some basic usages:

1. Create a hub connection:

// Import the library.
import 'package:signalr_client/signalr_client.dart';

// The location of the SignalR Server.
final serverUrl = "192.168.10.50:51001";
// Creates the connection by using the HubConnectionBuilder.
final hubConnection = HubConnectionBuilder().withUrl(serverUrl).build();
// When the connection is closed, print out a message to the console.
final hubConnection.onclose( (error) => print("Connection Closed"));

Logging is supported via the dart logging package:

// Import theses libraries.
import 'package:logging/logging.dart';
import 'package:signalr_client/signalr_client.dart';

// Configer the logging
Logger.root.level = Level.ALL;
// Writes the log messages to the console
Logger.root.onRecord.listen((LogRecord rec) {
  print('${rec.level.name}: ${rec.time}: ${rec.message}');
});

// If you want only to log out the message for the higer level hub protocol:
final hubProtLogger = Logger("SignalR - hub");
// If youn want to also to log out transport messages:
final transportProtLogger = Logger("SignalR - transport");

// The location of the SignalR Server.
final serverUrl = "192.168.10.50:51001";
final connectionOptions = HttpConnectionOptions
final httpOptions = new HttpConnectionOptions(logger: transportProtLogger);
//final httpOptions = new HttpConnectionOptions(logger: transportProtLogger, transport: HttpTransportType.WebSockets); // default transport type.
//final httpOptions = new HttpConnectionOptions(logger: transportProtLogger, transport: HttpTransportType.ServerSentEvents);
//final httpOptions = new HttpConnectionOptions(logger: transportProtLogger, transport: HttpTransportType.LongPolling);

// If you need to authorize the Hub connection than provide a an async callback function that returns 
// the token string (see AccessTokenFactory typdef) and assigned it to the accessTokenFactory parameter:
// final httpOptions = new HttpConnectionOptions( .... accessTokenFactory: () async => await getAccessToken() ); 

// Creates the connection by using the HubConnectionBuilder.
final hubConnection = HubConnectionBuilder().withUrl(serverUrl, options: httpOptions).configureLogging(hubProtLogger).build();
// When the connection is closed, print out a message to the console.
final hubConnection.onclose( (error) => print("Connection Closed"));

2. Connect to a Hub:

Calling following method starts handshaking and connects the client to SignalR server

await hubConnection.start();

3. Calling a Hub function:

Assuming there is this hub function:

public string MethodOneSimpleParameterSimpleReturnValue(string p1)
{
  Console.WriteLine($"'MethodOneSimpleParameterSimpleReturnValue' invoked. Parameter value: '{p1}");
  return p1;
}

The client can invoke the function by using:


  final result = await hubConnection.invoke("MethodOneSimpleParameterSimpleReturnValue", args: <Object>["ParameterValue"]);
  logger.log(LogLevel.Information, "Result: '$result");

4. Calling a client function:

Assuming the server calls a function "aClientProvidedFunction":

  await Clients.Caller.SendAsync("aClientProvidedFunction", null);

The Client provides the function like this:

  
  hubConnection.on("aClientProvidedFunction", _handleAClientProvidedFunction);

  // To unregister the function use:
  // a) to unregister a specific implementation:
  // hubConnection.off("aClientProvidedFunction", method: _handleServerInvokeMethodNoParametersNoReturnValue);
  // b) to unregister all implementations:
  // hubConnection.off("aClientProvidedFunction");
  ...
  void _handleAClientProvidedFunction(List<Object> parameters) {
    logger.log(LogLevel.Information, "Server invoked the method");
  }

A note about the parameter types #

All function parameters and return values are serialized/deserialized into/from JSON by using the dart:convert package (json.endcode/json.decode). Make sure that you:

  • use only simple parameter types

or

  • use objects that implements toJson() since that method is used by the dart:convert package to serialize an object.

Flutter Json 101:

[0.1.7-dev.11] #

  • Added more output in websocket

[0.1.7-dev.10] #

  • Added more output in websocket

[0.1.7-dev.9] #

  • Ported changes to websocket

[0.1.7-dev.8] #

  • Do not fail connection on invocation of client method

[0.1.7-dev.7] #

  • Fix to server timeout timer

[0.1.7-dev.6] #

  • Fix to server timeout timer

[0.1.7-dev.5] #

  • Changes to handshake completer

[0.1.7-dev.4] #

  • Fix for reconnect wait

[0.1.7-dev.3] #

  • Fixed null logger

[0.1.7-dev.2] #

  • Fixed delay during reconnect

[0.1.7-dev.1] #

  • Added draft of automatic reconnect

[0.1.5] #

  • Fix complex object serialization to json.

[0.1.4] #

  • Prevent null exception when calling HubConnection.stop()

[0.1.3] #

  • Change the logging behaviour: The client uses the dart standard logging package instead of a proprietary logging behaviour (see readme for an example).
  • Fixes a bug within the MessageHeaders class.

[0.1.2] #

  • Be more descriptional within the desciption of the pubspec.yaml

[0.1.1] #

  • Be more descriptional within the desciption of the pubspec.yaml

[0.1.0] #

  • Chat client/server example added.
  • Reformat Library code to compile to PUB spec.
  • Added some more description to the readme.

[0.0.1] #

  • Intitial Version

Use this package as a library

1. Depend on it

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


dependencies:
  signalr_client_mrj: ^0.1.7-dev.11

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:signalr_client_mrj/abort_controller.dart';
import 'package:signalr_client_mrj/dartio_http_client.dart';
import 'package:signalr_client_mrj/default_retry_policy.dart';
import 'package:signalr_client_mrj/errors.dart';
import 'package:signalr_client_mrj/handshake_protocol.dart';
import 'package:signalr_client_mrj/http_connection.dart';
import 'package:signalr_client_mrj/http_connection_options.dart';
import 'package:signalr_client_mrj/hub_connection.dart';
import 'package:signalr_client_mrj/hub_connection_builder.dart';
import 'package:signalr_client_mrj/iconnection.dart';
import 'package:signalr_client_mrj/ihub_protocol.dart';
import 'package:signalr_client_mrj/iretry_policy.dart';
import 'package:signalr_client_mrj/itransport.dart';
import 'package:signalr_client_mrj/json_hub_protocol.dart';
import 'package:signalr_client_mrj/long_polling_transport.dart';
import 'package:signalr_client_mrj/server_sent_events_transport.dart';
import 'package:signalr_client_mrj/signalr_client.dart';
import 'package:signalr_client_mrj/signalr_http_client.dart';
import 'package:signalr_client_mrj/text_message_format.dart';
import 'package:signalr_client_mrj/utils.dart';
import 'package:signalr_client_mrj/web_socket_transport.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
46
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
75
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • signalr_client_mrj that is a package requiring null.

Package not compatible with runtime flutter-web on web

Because:

  • package:signalr_client_mrj/dartio_http_client.dart that imports:
  • package:signalr_client_mrj/utils.dart that imports:
  • package:signalr_client_mrj/signalr_client.dart that imports:
  • package:signalr_client_mrj/hub_connection_builder.dart that imports:
  • package:signalr_client_mrj/http_connection.dart that imports:
  • package:signalr_client_mrj/web_socket_transport.dart that imports:
  • dart:io

Health suggestions

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

Analysis of lib/web_socket_transport.dart reported 2 hints:

line 16 col 8: The value of the field '_logMessageContent' isn't used.

line 64 col 16: This function has a return type of 'Future

Format lib/default_retry_policy.dart.

Run flutter format to format lib/default_retry_policy.dart.

Format lib/hub_connection.dart.

Run flutter format to format lib/hub_connection.dart.

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/hub_connection_builder.dart (Run flutter format to format lib/hub_connection_builder.dart.)
  • lib/iconnection.dart (Run flutter format to format lib/iconnection.dart.)
  • lib/ihub_protocol.dart (Run flutter format to format lib/ihub_protocol.dart.)
  • lib/iretry_policy.dart (Run flutter format to format lib/iretry_policy.dart.)
  • lib/json_hub_protocol.dart (Run flutter format to format lib/json_hub_protocol.dart.)

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and signalr_client_mrj.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

The description is too long. (-10 points)

Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
logging ^0.11.3+2 0.11.4
w3c_event_source ^1.3.0 1.3.2
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test