All Projects → stephen-moyer → ngx-signalr-hubservice

stephen-moyer / ngx-signalr-hubservice

Licence: MIT license
Makes using SignalR in Angular 2/4 easy

Programming Languages

powershell
5483 projects
typescript
32286 projects
C#
18002 projects
HTML
75241 projects

Projects that are alternatives of or similar to ngx-signalr-hubservice

Sapphiredb
SapphireDb Server, a self-hosted, easy to use realtime database for Asp.Net Core and EF Core
Stars: ✭ 326 (+1258.33%)
Mutual labels:  aspnet, signalr
Server
The core infrastructure backend (API, database, Docker, etc).
Stars: ✭ 8,797 (+36554.17%)
Mutual labels:  aspnet, signalr
Aspnetcore Angular Universal
ASP.NET Core & Angular Universal advanced starter - PWA w/ server-side rendering for SEO, Bootstrap, i18n internationalization, TypeScript, unit testing, WebAPI REST setup, SignalR, Swagger docs, and more! By @TrilonIO
Stars: ✭ 1,455 (+5962.5%)
Mutual labels:  aspnet, angular2
UXsyncNow
UXstorm's ServiceNow file synchronizer / development tool
Stars: ✭ 23 (-4.17%)
Mutual labels:  angular2
AuthGuard
Example repo for guarding routes post
Stars: ✭ 42 (+75%)
Mutual labels:  angular2
live-share-editor
ソースコードをリアルタイムで共有できるオンラインエディタ
Stars: ✭ 15 (-37.5%)
Mutual labels:  signalr
ng2-right-click-menu
Right click context menu for Angular 2+
Stars: ✭ 51 (+112.5%)
Mutual labels:  angular2
pokeR
Planning poker with SignalR
Stars: ✭ 37 (+54.17%)
Mutual labels:  signalr
identityazuretable
This project provides a high performance cloud solution for ASP.NET Identity Core using Azure Table storage replacing the Entity Framework / MSSQL provider.
Stars: ✭ 97 (+304.17%)
Mutual labels:  aspnet
ngx-tabset
A very simple library to let you create some tabs
Stars: ✭ 19 (-20.83%)
Mutual labels:  angular2
angular-2-admin
An Angular 2 Admin Dashboard built using the Angular-CLI.
Stars: ✭ 65 (+170.83%)
Mutual labels:  angular2
ng2-mqtt-demo
Angular 2 demo using MQTT.js in Typescript
Stars: ✭ 32 (+33.33%)
Mutual labels:  angular2
serverlessnotifications
Serverless notifications with Azure Cosmos DB + Azure Functions + Azure SignalR
Stars: ✭ 60 (+150%)
Mutual labels:  signalr
ic-datepicker
Angular (2+) datepicker component
Stars: ✭ 27 (+12.5%)
Mutual labels:  angular2
book-monkey2
🐵 Demo-Projekt zum Buch (1. Auflage)
Stars: ✭ 36 (+50%)
Mutual labels:  angular2
cloud-speech-and-vision-demos
A set of demo applications that make use of google speech, nlp and vision apis based in angular2
Stars: ✭ 35 (+45.83%)
Mutual labels:  angular2
nativescript-ng2-drawer-seed
Nativescript template project with drawer support
Stars: ✭ 17 (-29.17%)
Mutual labels:  angular2
Dianoia-app
Mobile (Ionic 3 - Angular 4) app about non-pharmaceutical activities and information for people with dementia.
Stars: ✭ 13 (-45.83%)
Mutual labels:  angular2
ng2-carouselamos
Simple carousel/slider for angular 2.
Stars: ✭ 39 (+62.5%)
Mutual labels:  angular2
ng-observe
Angular reactivity streamlined...
Stars: ✭ 65 (+170.83%)
Mutual labels:  angular2

ngx-signalr-hubservice

Makes using SignalR in Angular 2/4 easy.

Getting started

  1. Set up a new project using @angular/cli

  2. Install ngx-signalr-hubservice npm install --save ngx-signalr-hubservice

  3. Add the jquery and SignalR scripts to your angular-cli.json

    "scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/signalr/jquery.signalr.js"]
  4. Import the HubService and add it to your providers in your app.module.ts. Mine looks like this(I've included FormsModule for the demo):

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    
    import { HubService } from 'ngx-signalr-hubservice';
    
    @NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        FormsModule
    ],
    providers: [ HubService ],
    bootstrap: [AppComponent]
    })
    export class AppModule { }
  5. Inject the hub service in (probably) your root component. In this case it's app.component.ts. Make sure you import the service. Here you can make the connection.

    constructor(private hubService: HubService) {
    }
    ...
    ...
    async ngOnInit() {
        // connects to the SignalR server.
    	// passing in null for options will just use /signalr on the current domain as the url
        this.connected = await this.hubService.connect().toPromise();
    }

    For my applications I generally just reference the HubService in my other services(one service for each hub), and expose the hub methods/events through those services. For a demo this works too though.

  6. Define a class that will interact with the SignalR server. For me it's just the root AppComponent. You can use the @Hub decorator on the class to define what hub this class connects to.

    import { Component, OnInit } from '@angular/core';
    import { 
        HubService, 
        Hub, 
        HubSubscription, 
        HubWrapper 
    } from 'ngx-SignalR-hubservice';
    
    import 'rxjs/add/operator/toPromise';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    @Hub({ hubName: 'chatHub' }) // <-- Your hub declaration
    export class AppComponent implements OnInit {
    
        private hubWrapper: HubWrapper;
    
        connected = false;
    
        constructor(private hubService: HubService) {
            this.hubWrapper = hubService.register(this);
        }
    
        async ngOnInit() {
            this.connected = await this.hubService.connect().toPromise();
        }
    
    }
  7. Define a method that the hub will call with @HubSubscription in the same class. You can pass in the method name in the decorator, or just leave it blank. If left blank, the service will use the name of the method that you added the decorator to as the subscription name. NOTE: you generally have to run these callbacks inside angular's zone if you're updating UI. Hopefully future versions you won't have to do this.

    @HubSubscription()
    receiveMessage(param1: any, param2: any) {
        console.log(param1, param2);
    }
  8. For calling methods on the hub, you need to register this class with the hub service.

    Update your constructor to this, and add a new field on your class:

    private hubWrapper: HubWrapper;
    constructor(private hubService: HubService) {
        this.hubWrapper = hubService.register(this);
    }

    Now you can call methods on the hub using this hub wrapper,

    callHubMethod() {
        var result = await this.hubWrapper.invoke<boolean>('methodName', 'someParam').toPromise();
        console.log(result);
    }
  9. You can unregister hub wrappers from the service with hubWrapper.unregister() or hubService.unregister(this);. Generally you wouldn't want to do this because you'll call SignalR from services that exist during the lifetime of your application.

    ngOnDestroy() {
        this.hubWrapper.unregister();
        //or this.hubService.unregister(this);
    }

Hub Groups

You can use the hubGroups parameter on the @Hub decorator if you have two or more SignalR connections being made and want to control which @Hub decorators are applying to which connection.

You can see an example of this here:

@Injectable()
@Hub({ hubName: 'DataHub', hubGroup: 'group1' })
export class DataHubService {

    constructor (@Inject(HUB_SERVICE_GROUP1) private hubService: HubService) {
        this.hubWrapper = this.hubService.register(this);
        this.hubService.connect('http://localhost:81/signalr', { hubGroup: 'group1' }).toPromise();
    }

}

@Injectable()
@Hub({ hubName: 'EvaluationHub', hubGroup: 'group2' })
export class EvaluationHubService {

    constructor (@Inject(HUB_SERVICE_GROUP2) private hubService: HubService) {
        this.hubWrapper = this.hubService.register(this);
        this.hubService.connect('http://localhost:82/signalr', { hubGroup: 'group2' }).toPromise();
    }

}

Note the @Inject decorator on the constructors of the services. You need to specify which connection you want to inject into your service if you're using multiple HubServices. Remember to provide your HubService's correctly too with the proper InjectionToken

// Probably at the top of your NgModule
export let HUB_SERVICE_GROUP1 = new InjectionToken<HubService>("hubservice.group1");
export let HUB_SERVICE_GROUP2 = new InjectionToken<HubService>("hubservice.group2");

...

// In your NgModule decorator
providers: [
    { provide: HUB_SERVICE_GROUP1, useValue: new HubService() }
    { provide: HUB_SERVICE_GROUP2, useValue: new HubService() }
]

Notes

  • If you want to get the underlying SignalR instances, you can access them through HubService.connection for the SignalR connection instance($.connection). You can access the SignalR hub instances for the individual hubs through HubWrapper.hub.

  • If you pass attemptReconnects as true to HubService.connect options parameter, any invoke calls on your HubWrappers will defer until the HubService reconnects. They will most likely not error.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].