Socket.IO quickstart


Install express:

$ npm install express

Create a basic express app:

// index.js
const express = require('express');
const http = require('http');

const app = express();
const server = http.Server(app);

const PORT = 3000;
server.listen(PORT, () => console.log('Listening on *:' + PORT));

Create directory public, which will serve static files:

$ mkdir public

Create index.html:

$ echo 'Hello world' > public/index.html

Update the express app to serve the static directory:

// index.js
// ...

Start the server:

$ node index.js

Go to localhost:3000 in your browser to see Hello world:

$ open http://localhost:3000/



$ npm install

Add Socket.IO to the server:

// index.js
// ...
const Server = require('');
const io = new Server(server);

io.on('connection', socket => {
  console.log('User connected:',;

  socket.on('disconnect', () => {
    console.log('User disconnected:',;

Add Socket.IO to the client:

<!-- public/index.html -->
<script src="/"></script>
  var socket = io();
  socket.on('connect', function() {
    console.log('User connected');

Restart the server by pressing Ctrl-C in your terminal and then running npm start again.

After refreshing the browser page, the messages should show up in your browser and terminal console.

Now you can start sending and receiving events!


See demo:

Please support this site and join our Discord!