create a socket.io server node js code example
Example 1: create a web server node js w socket.io
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
// Ran when a GET request to path '/'
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
// Ran when a socket connected
});
http.listen(3000, () => {
// Ran when server is ready to take requestes
});
Example 2: socket io nodejs
const express = require('express');const app = express();const http = require('http');const server = http.createServer(app);const { Server } = require("socket.io");const io = new Server(server);app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html');});io.on('connection', (socket) => { console.log('a user connected');});server.listen(3000, () => { console.log('listening on *:3000');});