Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors when importing socket.io-client in Angular 2 app [duplicate]

I'm trying to use the socket.io client in my angular 2 app and installed it and typings

I'm just importing from 'socket.io-client';

but somehow I get tons of errors from that:

enter image description here

I can use the lib in my index.html if I load the script from a cdn and just put the init code in a <script> tag but I can't use it in my actual angular 2 app.

What am I doing wrong here?

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

The examples for socket.io seem outdated which is why I avoided them.

this is all i'm doing and already getting those errors:

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

So the issue seems to be related to SystemJS in someway. This https://github.com/mgechev/angular2-seed/wiki/Add-external-dependency suggests I can add the socket.io-client and it should add all dependencies automaticly that doesn't seem the case though.

I've tried the full example but that won't work either.

like image 796
gempir Avatar asked Aug 06 '16 06:08

gempir


1 Answers

Im using socket.io client in my angular 2 application, and have no problems with it. First of all you should not put script tag to socket.io-client in your index.hml. Second 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 simply use it:

import * as io from "socket.io-client";
io.connect(url, { /* ... */ });
like image 120
pleerock Avatar answered Oct 21 '22 03:10

pleerock