6

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:

  1. .NET Core Web API for API access
  2. List item

MS SQL DB for the database

1
  • 1
    Because new WebSocket('http://www.example.com') doesn't need a module. Commented Jul 1, 2017 at 3:18

4 Answers 4

5

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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:

https://www.websocket.org/

2 Comments

My backend database is MS SQL DB, not Firebase.
That really doesn't matter. I've integrated Firebase login and push into apps without using it as my database. But that aside, web sockets isn't really complicated to implement at all, you could easily create a module/service. Again check out websocket.org. You still have to do the backend work, but again there's all kinds of packages out there for that, python etc.
1

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.

Comments

0

i use angular2-websocket/angular2-websocket in my angular project, and it work very well https://github.com/afrad/angular2-websocket

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.