Socket.io-client in Angular 2 application

I am trying to use the socket.io client in my angular 2 application and installed it and typings

I just import from 'socket.io-client';

but somehow I get a ton of errors from this:

enter image description here

I can use lib in my index.html if I load the script from cdn and just put the init code in the <script> , but I cannot use it in my actual angular 2 application.

What am I doing wrong here?

This is my template: https://github.com/mgechev/angular2-seed

The examples for socket.io look deprecated, so I avoided them.

That is all I am doing and already getting these errors:

 import * as io from 'socket.io-client'; [...] var socket = io('127.0.0.1'); 

So the problem seems to be related to SystemJS in some way. This https://github.com/mgechev/angular2-seed/wiki/Add-external-dependency assumes that I can add socket.io-client and it should automatically add all the dependencies, which doesn't look like this.

I tried a complete example, but that won't work either.

+7
angularjs angular
source share
2 answers

Im using the socket.io client in my angular 2 application and you have no problem with it. First of all, you should not put the script tag in socket.io-client in your index.hml . Secondly, you need these lines in your system.js configuration:

 { packages: { "socket.io-client": {"defaultExtension": "js"} }, map: { "socket.io-client": "node_modules/socket.io-client/socket.io.js" } } 

Then you just use it:

 import * as io from "socket.io-client"; io.connect(url, { /* ... */ }); 
+2
source share

based on this discussed in question you should use npm install @types/socket.io-client --save

0
source share

All Articles