Adam Harte
Adam Harte

Reputation: 10530

How to use Socket.IO in TypeScript with Visual Studio Code?

I am looking for a simple example of how to get Socket.IO working with TypeScript while using Visual Studio Code. I am running on Node.js.

I have installed the d.ts for socket.io, and it appears in the typings folder.

cd src/
tsd query socket.io --action install

I have added socket.io to the package.json dependencies.

"dependencies": {
    "socket.io" : "*"
}

The run npm install, and the package files are showing in the node_modules directory.

I then try to import socket.io in TypeScript, and here is where I don't know what I am doing. I tried this:

import * as sio from 'socket.io';

It doesn't complain about anything, but when I just to use sio it does not give me any IntelliSense. So I feel like I have done something wrong.

Is that the correct import? Are there any up to date examples around?

Upvotes: 2

Views: 14550

Answers (1)

basarat
basarat

Reputation: 276343

import * as sio from 'socket.io';

That is the right import. You can see it in use here : https://github.com/TypeScriptBuilder/tsb/blob/ac87f359201a25e1bbb44f72093487348f3d28d3/src/socketLib/socketLibServer.ts#L2 Try restarting VS code.

Are there any up to date examples around?

I use socket.io quite heavily here : https://github.com/TypeScriptBuilder/tsb

Upvotes: 10

Related Questions