How can I use web-sockets with Angular4? The HttpModule does not support it, so is there a NPM package available that I can use?
Set Up:
- .NET Core Web API for API access
- List item
MS SQL DB for the database
How can I use web-sockets with Angular4? The HttpModule does not support it, so is there a NPM package available that I can use?
Set Up:
MS SQL DB for the database
Angular doesn't offer a special object to support WebSockets. But the good news is that WebSocket is supported by all browsers, and you can use it in your Angular4 apps. In this app I was using the ws package on the server (Node.js)and the browser's WebSocket object on the client (see https://github.com/Farata/angular2typescript/blob/master/Angular4/auction/client/src/app/product-detail/websocket.service.ts)
One option, you can always use Firebase (this btw is a very interesting option). Pretty much does all the heavy lifting for you.
Otherwise, WebSockets is an HTML5 standard. You just have to write up some code to implement it. Check out this website which has all kinds of info, an echo demo/test, etc:
You can use the socket.io-client, just be sure to include the typings when installing.
https://www.npmjs.com/package/socket.io-client
Install:
npm install --save socket.io-client
and
npm install --save @types/socket.io-client
or npm install --save-dev @types/socket.io-client
note: There's a great tutorial that includes guidance for a MEAN full-stack environment here if interested.
edit: As others have mentioned WebSocket is an HTML5 standard. This will simply create an easy to use interface for front-end socket interactions and is applicable for any back-end environment.
i use angular2-websocket/angular2-websocket in my angular project, and it work very well https://github.com/afrad/angular2-websocket
new WebSocket('http://www.example.com')
doesn't need a module.