Server Initialization
Once you have installed the Socket.IO server library, you can now init the server. The complete list of options can be found here.
For TypeScript users, it is possible to provide type hints for the events. Please check this.
Initialization
Standalone
- CommonJS
- ES modules
- TypeScript
const{Server}=require("socket.io");
const io =newServer({/* options */});
io.on("connection",(socket)=>{
// ...
});
io.listen(3000);
import{Server}from"socket.io";
const io =newServer({/* options */});
io.on("connection",(socket)=>{
// ...
});
io.listen(3000);
import{ Server }from"socket.io";
const io =newServer({/* options */});
io.on("connection",(socket)=>{
// ...
});
io.listen(3000);
You can also pass the port as the first argument:
- CommonJS
- ES modules
- TypeScript
const{Server}=require("socket.io");
const io =newServer(3000,{/* options */});
io.on("connection",(socket)=>{
// ...
});
import{Server}from"socket.io";
const io =newServer(3000,{/* options */});
io.on("connection",(socket)=>{
// ...
});
import{ Server }from"socket.io";
const io =newServer(3000,{/* options */});
io.on("connection",(socket)=>{
// ...
});
This implicitly starts a Node.js HTTP server, which can be accessed through io.httpServer.
With an HTTP server
- CommonJS
- ES modules
- TypeScript
const{ createServer }=require("http");
const{Server}=require("socket.io");
const httpServer =createServer();
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import{ createServer }from"http";
import{Server}from"socket.io";
const httpServer =createServer();
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import{ createServer }from"http";
import{ Server }from"socket.io";
const httpServer =createServer();
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
With an HTTPS server
- CommonJS
- ES modules
- TypeScript
const{ readFileSync }=require("fs");
const{ createServer }=require("https");
const{Server}=require("socket.io");
const httpServer =createServer({
key:readFileSync("/path/to/my/key.pem"),
cert:readFileSync("/path/to/my/cert.pem")
});
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import{ readFileSync }from"fs";
import{ createServer }from"https";
import{Server}from"socket.io";
const httpServer =createServer({
key:readFileSync("/path/to/my/key.pem"),
cert:readFileSync("/path/to/my/cert.pem")
});
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import{ readFileSync }from"fs";
import{ createServer }from"https";
import{ Server }from"socket.io";
const httpServer =createServer({
key:readFileSync("/path/to/my/key.pem"),
cert:readFileSync("/path/to/my/cert.pem")
});
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
See also: Node.js documentation
With an HTTP/2 server
- CommonJS
- ES modules
- TypeScript
const{ readFileSync }=require("fs");
const{ createSecureServer }=require("http2");
const{Server}=require("socket.io");
const httpServer =createSecureServer({
allowHTTP1:true,
key:readFileSync("/path/to/my/key.pem"),
cert:readFileSync("/path/to/my/cert.pem")
});
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import{ readFileSync }from"fs";
import{ createSecureServer }from"http2";
import{Server}from"socket.io";
const httpServer =createSecureServer({
allowHTTP1:true,
key:readFileSync("/path/to/my/key.pem"),
cert:readFileSync("/path/to/my/cert.pem")
});
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import{ readFileSync }from"fs";
import{ createSecureServer }from"http2";
import{ Server }from"socket.io";
const httpServer =createSecureServer({
allowHTTP1:true,
key:readFileSync("/path/to/my/key.pem"),
cert:readFileSync("/path/to/my/cert.pem")
});
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
See also: Node.js documentation
With Express
- CommonJS
- ES modules
- TypeScript
const express =require("express");
const{ createServer }=require("http");
const{Server}=require("socket.io");
const app =express();
const httpServer =createServer(app);
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
importexpressfrom"express";
import{ createServer }from"http";
import{Server}from"socket.io";
const app =express();
const httpServer =createServer(app);
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import*as express from"express";
import{ createServer }from"http";
import{ Server }from"socket.io";
const app =express();
const httpServer =createServer(app);
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
Using app.listen(3000) will not work here, as it creates a new HTTP server.
More information here.
With Koa
- CommonJS
- ES modules
- TypeScript
constKoa=require("koa");
const{ createServer }=require("http");
const{Server}=require("socket.io");
const app =newKoa();
const httpServer =createServer(app.callback());
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
importKoafrom"koa";
import{ createServer }from"http";
import{Server}from"socket.io";
const app =newKoa();
const httpServer =createServer(app.callback());
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
import*as Koa from"koa";
import{ createServer }from"http";
import{ Server }from"socket.io";
const app =newKoa();
const httpServer =createServer(app.callback());
const io =newServer(httpServer,{/* options */});
io.on("connection",(socket)=>{
// ...
});
httpServer.listen(3000);
More information here.
With Nest
See the documentation here.
NestJS v7 and below relies on Socket.IO v2, while NestJS v8 relies on Socket.IO v4. Please use a compatible client.
With Fastify
You need to register the fastify-socket.io plugin:
- CommonJS
- ES modules
- TypeScript
const fastify =require("fastify");
const fastifyIO =require("fastify-socket.io");
const server =fastify();
server.register(fastifyIO);
server.get("/",(req, reply)=>{
server.io.emit("hello");
});
server.ready().then(()=>{
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection",(socket)=>{
// ...
});
});
server.listen(3000);
importfastifyfrom"fastify";
importfastifyIOfrom"fastify-socket.io";
const server =fastify();
server.register(fastifyIO);
server.get("/",(req, reply)=>{
server.io.emit("hello");
});
server.ready().then(()=>{
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection",(socket)=>{
// ...
});
});
server.listen(3000);
import fastify from"fastify";
import fastifyIO from"fastify-socket.io";
const server =fastify();
server.register(fastifyIO);
server.get("/",(req, reply)=>{
server.io.emit("hello");
});
server.ready().then(()=>{
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection",(socket)=>{
// ...
});
});
server.listen(3000);
With μWebSockets.js
import{App}from"uWebSockets.js";
import{Server}from"socket.io";
const app =newApp();
const io =newServer();
io.attachApp(app);
io.on("connection",(socket)=>{
// ...
});
app.listen(3000,(token)=>{
if(!token){
console.warn("port already in use");
}
});
Reference: https://github.com/uNetworking/uWebSockets.js
Options
The complete list of available options can be found here.