Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using socket.io-client in angular-cli@webpack

I'm tying to use socket.io in my Angular 2 application, and I'm not having much luck getting access to it from within my Angular 2 application. Here's some information about my setup:

Angular 2: rc.5
angular-cli: 1.0.0-beta.11-webpack.2
Node: 6.2.2
OS: Linux x64

I've got socket.io installed via npm and typings, and I see it in my node_modules directory, but that Angular 2 application can't seem to resolve:

import * as io from 'socket.io-client';

I've seen a posts about how to address this using Webpack and its config files, but those don't seem to exist for an application generated with angular-cli@webpack.

like image 329
writes_on Avatar asked Jul 28 '26 14:07

writes_on


1 Answers

All you need should be the following:

npm:

npm install socket.io-client --save

import in your component.ts/service.ts

import * as io from 'socket.io-client';

a dummy module declaration in your typings.d.ts:

declare module 'socket.io-client' {
  var e: any;
  export = e;
}

I don't know if typings are available, but you can try npm i @types/socket.io-client --save-dev instead of just declaring a dummy.

Any webpack config should be taken care of by angular-cli.

like image 111
j2L4e Avatar answered Jul 30 '26 08:07

j2L4e