Performance: Socket.io integration with Express
This affects the real-time communication speed and server response time, impacting how fast users see updates and interact with the app.
import express from 'express'; import { createServer } from 'http'; import { Server } from 'socket.io'; const app = express(); const httpServer = createServer(app); const io = new Server(httpServer, { cors: { origin: '*' } }); app.get('/', (req, res) => { res.sendFile(new URL('./index.html', import.meta.url).pathname); }); io.on('connection', (socket) => { socket.on('message', (msg) => { socket.broadcast.emit('message', msg); }); }); httpServer.listen(3000);
const express = require('express'); const app = express(); const http = require('http').createServer(app); const io = require('socket.io')(http); app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); }); io.on('connection', (socket) => { socket.on('message', (msg) => { io.emit('message', msg); }); }); http.listen(3000);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default Socket.io with Express HTTP server | N/A (server-side) | N/A | N/A | [!] OK |
| Separated HTTP server with optimized Socket.io config | N/A (server-side) | N/A | N/A | [OK] Good |
const http = require('http');
const express = require('express');
const app = express();
const server = http.createServer(app);
const { Server } = require('socket.io');
const io = new Server(server);
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
});
server.listen(3000);const express = require('express');
const app = express();
const io = require('socket.io')(app);
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);io.on('connection', (socket) => {
// Broadcast 'hello' to others
???
});Pass the test and this pattern moves unknown