Ibanez1408
Ibanez1408

Reputation: 5058

Constructor of class 'HubConnection' is private and only accessible within the class declaration

I am trying to implement an angular and signalr project. I am getting the sample from Medium

I have installed all the necessary packages and have the code below in the app.component.ts:

import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr';

import { Message } from 'primeng/api';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'PROJECT TITLE';
  base_url = 'http://localhost:8081/';

  hubConnecton: HubConnection;
  msgs: Message[] = [];

  constructor() { 

  }

  ngOnInit(): void {
    this.hubConnecton = new HubConnection(this.base_url);
  }
}

But I get an error in the = new HubConnection(this.base_url); with this message: Constructor of class 'HubConnection' is private and only accessible within the class declaration.

enter image description here

I need your help with this please. Thank you.

Upvotes: 12

Views: 6480

Answers (2)

Virag Shakhida
Virag Shakhida

Reputation: 216

You have to use HubConnectionBuilder().

import { Component, OnInit } from '@angular/core';
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';
import { Message } from 'primeng/api';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  public _hubConnecton: HubConnection;
  msgs: Message[] = [];

  constructor() { }

  ngOnInit(): void {
    this._hubConnecton = new HubConnectionBuilder().withUrl("http:/:localhost:1874/notify").build();
    this._hubConnecton
      .start()
      .then(() => console.log('Connection started!'))
      .catch(err => console.log('Error while establishing connection :('));

    this._hubConnecton.on('BroadcastMessage', (type: string, payload: string) => {
      this.msgs.push({ severity: type, summary: payload });
    });
  }
}

Upvotes: 20

Brennan
Brennan

Reputation: 1933

The intended method for creating a HubConnection is to use the HubConnectionBuilder API. You can check out the docs for more specifics https://learn.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-2.1

Upvotes: 0

Related Questions