Angular WebSocket Universal Server Rendering

Are there any Angular Universal examples with WebSockets?

The Serveride implementation does not know what the WebSocket object is in my case. And if I use socket.io, the node server freezes when trying to make connections.

Additional information about the problem:

I downloaded angular -universal-starter from github: https://github.com/angular/universal-starter which works just fine from the box running "npm install" and "npm start"

But after I added the following code to the AppComponent

   export class AppComponent implements OnInit {
       ngOnInit() {
           let webSocket = new WebSocket("----server url----")
       }
   }

I got the following error on the NodeJs server console:

EXCEPTION: WebSocket is not defined
ORIGINAL STACKTRACE:
ReferenceError: WebSocket is not defined
    at AppComponent.ngOnInit (/Volumes/Development/cacadu/website/universal-starter-master2/dist
/server/index.js:41725:29)
+4
source share
2

websocket , , ,

import { isBrowser, isNode } from 'angular2-universal';

, !

export class AppComponent implements OnInit {
    ngOnInit() {
        if (isBrowser) {
            let webSocket = new WebSocket("----server url----");
        }
    }
}
+1

:

angular -universal-starter github: https://github.com/angular/universal-starter , "npm install" "npm start"

, AppComponent

   export class AppComponent implements OnInit {
       ngOnInit() {
           let webSocket = new WebSocket("----server url----")
       }
   }

NodeJs:

EXCEPTION: WebSocket is not defined
ORIGINAL STACKTRACE:
ReferenceError: WebSocket is not defined
    at AppComponent.ngOnInit (/Volumes/Development/cacadu/website/universal-starter-master2/dist
/server/index.js:41725:29)
0

Source: https://habr.com/ru/post/1667021/


All Articles