3

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.

1 Answer 1

8

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.

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

1 Comment

Thanks for your response, I've included these steps in my project and it seems to be working. At least to the point where I can include socket.io in my service and create a socket, and things don't blow up! I'm going to make an alternative to my current polling/http service that uses socket.io and see if it all hangs together. Again, thanks for your help!!

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.