const io = require('socket.io')(); io.on('connection', (socket) => { console.log('a user connected'); socket.on('disconnect', () => { console.log('user disconnected'); }); }); io.listen(3000);
const socket = io(); socket.on('connect', () => { console.log('connected to server'); }); socket.on('message', (data) => { console.log(`received message: ${data}`); }); socket.emit('message', 'hello, server!');This example shows how to create a socket.io client that connects to a server and sends a message. When the client connects, it logs a message, and when it receives a message from the server, it logs another message. Package library: Socket.io is a package that is available on npm, and it can be installed using the npm package manager.