Skip to content

ArchetypicalSoftware/auto-hub-connection

Repository files navigation

AutoHubConnection

AutoHubConnection is a simple wrapper to the .net core signalr client implementation. It automates the logic necessary to reconnecting after a unexpected disconnect.

Installation

Install globally via npm:

npm install -g @archetypical/auto-hub-connection

Or globally via yarn:

yarn global add @archetypical/auto-hub-connection

Usage

Once you have established your server side hub, you can connect to it with a new AutoHubConnection. It includes all the same methods provided by the signalr client with the same expected functionality.

In addition to the existing functionality, AutoHubConnection provides simple instantiation of a new connection and the logic to automatically reconnect when there is a network disconnect.

Instantiation

When creating a new AutoHubConnection, you must provide a host.

const autoHubConnection = new AutoHubConnection('/MyHubPath');

Optionally you can provide configuration settings used to change the reconnect behavior.

const autoHubConnection = new AutoHubConnection('/MyHubPath', {
    logLevel: LogLevel.Error,
    retryInterval: 3000,
    maxConnectionAttempts: 5
});

logLevel: The log level the underlying signalr client will log at. Default value is LogLevel.Error.

retryInterval: Defines the wait interval between connection attempts in milliseconds. Default value is 5000.

maxConnectionAttempts: Max number of attempts to initiate a connection. Default value is 5.

Example

const autoHubConnection = new AutoHubConnection('/MyHubUrl');
autoHubConnection.on('EventName', x => console.log(x));
autoHubConnection.start();

For more detail on how to utilize the signalr client, please reference the official documentation.

About

Auto reconnecting wrapper for the Signal HubConnection

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages