npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-signalr-websocket

v1.1.0-beta.10

Published

A lightweight ASP.NET SignalR client for Angular

Downloads

1,314

Readme

ngx-signalr-websocket

A lightweight RxJS library that allows you to connect to ASP.NET SignalR using WebSocket. It is designed to provide simpler API.

This is based on the SignalR specification and uses the classes compatible with Angular. This ensures a small size of the extra code and good tree-shaking support.

Features

  • Depends only on RxJS
  • Compatible with Angular HttpClient, but this is not necessary
  • Implements the usual reactive API for Angular developers
  • Provides good typing support
  • Allows to configure messages serialization
  • Allows to authorize using access token
  • Supports WebSockets transport and Text transfer format (JSON)

Dependencies

package | version --------------- | --------- rxjs | >= 7.0.0

Getting started

  1. Install ngx-signalr-websocket npm i --save ngx-signalr-websocket.

  2. Import SignalrClient and connect to SignalR hub:

    import { SignalrClient } from 'ngx-signalr-websocket';
    
    ...
    
    const client = SignalrClient.create(httpClient);
    // constructor is also available: new SignalrClient(httpClient);
       
    const connection = client.connect(signalrHubUri);
  3. Next, subscribe to invocations.

    connection.on<[TMessage]>('ReceiveMessage')
      .subscribe(([message]) => ...)
    
    ...
  4. Finally, when the job is done and you don`t need connection, you may disconnect:

    connection.disconnect();

Send message

To send messages to the server сlients call public methods on hubs via the invoke() method of the HubConnection. The invoke() method accepts:

  • the name of the hub method
  • any arguments defined in the hub method

In the following example, the method name on the hub is 'SendMessage'. The second and third arguments passed to invoke map to the hub method's 'user' and 'message' arguments:

connection.invoke<TData>('SendMessage', user, message)
  .subscribe(data => ...);

If you only need to send a message to the server, you can use the send() method. It does not wait for a response from the receiver.

connection.send<TData>('SendMessage', user, message);

Receive messages

To receive messages from the hub, define a method using the on() method of the SignalrConnection.

In the following example, the method name is 'ReceiveMessage'. The argument names are 'user' and 'message':

connection.on<[string, string]>('ReceiveMessage')
  .subscribe(([user, message]) => ...);

Streaming

Another way to get messages from the service is by streaming. Clients call server-to-client streaming methods on hubs with stream() method. The stream() method accepts two arguments:

  • The name of the hub method
  • Arguments defined in the hub method

It returns an Observable, which contains a subscribe method. In the following example, the hub method name is 'Counter' and the arguments are a count for the number of stream items to receive and the delay between stream items.

connection.stream<TItem>('Counter', 10, 500)
    .subscribe(item => ...);

To end the stream from the client, call the unsubscribe() method on the ISubscription that's returned from the subscribe method. Or wait until the server invokes CompletionMessage.

Configuration

With the default configuration, the client converts JSON as is.

If you want to override the configuration, you can use the constructor parameter:

SignalrClient.create(url, configuration => {
  confgiuration.propertyParsers = []; // This is default value.
})

If you need specific settings for message parsing, you can add functions (name: string, value: any) => any to the propertyParsers option. If you need a Date conversion, just add parseIsoDateStrToDate to propertyParsers.

Service example

The following example demonstrates the SignalR client usage in the Angular service.

It uses NgRx to provide SignalR Hub URL. In general, this is not necessary, but the example shows how it can be applied.

import { HttpClient } from '@angular/common/http';
import { Injectable, OnDestroy } from '@angular/core';
import { Store } from '@ngrx/store';
import { BehaviorSubject, filter, map, Observable, switchMap, withLatestFrom } from 'rxjs';
import { SignalrClient, SignalrConnection } from 'ngx-signalr-websocket';
import * as fromRoot from '@app/store/reducers';

...

@Injectable({
  providedIn: 'root'
})
export class AppSignalrService implements OnDestroy {

  private client: SignalrClient;
  private connection$ = new BehaviorSubject<SignalrConnection | null>(null);

  private readonly readyConnection$ = this.connection$.pipe(filter(connection => !!connection && connection.opened));

  constructor(store: Store<fromRoot.State>, httpClient: HttpClient) {

    this.client = SignalrClient.create(httpClient);

    store.select(fromRoot.selectSignalrHubUri)
      .pipe(
        switchMap(uri => this.client.connect(uri)),
        retryWhen(errors => errors.pipe(
          tap(error => console.error(`SignalR connection error: ${error}`)),
          delay(5000)
        )))
      .subscribe(connection => {
        this.disconnect();
        this.connection$.next(connection);
      });
  }

  getLastMessages(): Observable<string[]> {
    return this.readyConnection$
      .pipe(switchMap(connection => connection.invoke<string[]>('GetLastMessages', 10)));
  }

  sendMessage(user: string, message: string): void {
    return this.readyConnection$
      .pipe(switchMap(connection => connection.send('SendMessage', user, message)));
  }

  onReceiveMessage(): Observable<{ user: string, message: string }> {
    return this.readyConnection$
      .pipe(
        switchMap(connection => connection.on<[string, string]>('ReceiveMessage')),
        map(([user, message]) => { user, message }));
  }

  getUserMessagesStream(user: string): Observable<string> {
    return this.readyConnection$
      .pipe(switchMap(connection => connection.stream<string>('GetUserMessagesStream', user)));
  }

  ngOnDestroy(): void {
    this.disconnect();
  }

  private disconnect(): void {
    if (this.connection$.value) { this.connection$.value.close(); }
  }
}