'WebSocket connection failed - Angular

I'm trying to use webSocket as seen in here, but I'm getting an error as I try to subscribe for it.

I'm new to webSockets - here is my code at app.component.ts:

import { Component } from '@angular/core';
import { webSocket } from "rxjs/webSocket";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
    
})
export class AppComponent {
    constructor(){
        const subject = webSocket("ws://localhost:8081");
        subject.subscribe(
           msg => console.log('message received: ' + msg), // Called whenever there is a message from the server.
           err => console.log(err), // Called if at any point WebSocket API signals some kind of error.
           () => console.log('complete') // Called when connection is closed (for whatever reason).
        );
    }
}   

  
       

And i'm getting this error on console:

WebSocket connection to 'ws://localhost:8081/' failed: 
               

Why am I getting this error and subscription doesn't work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source