_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN
Let's talk

WebSocket — real-time komunikace

02. 03. 2024 1 min read intermediate

Architecture Intermediate

WebSocket — real-time komunikace

WebSocketReal-timeSocket.io 3 min read

Implementace WebSocket serveru pro chat, notifikace a live dashboardy. Škálování s Redis.

Kdy WebSocket?

Obousměrný, persistentní kanál. Server can pushovat data klientovi. Use cases: chat, notifikace, real-time dashboardy.

Socket.io example

// Server
const io = new Server(3000, { cors: { origin: '*' } });
io.on('connection', (socket) => {
    socket.on('join-room', (room) => socket.join(room));
    socket.on('message', (data) => {
        io.to(data.room).emit('message', {
            from: socket.id, text: data.text, ts: Date.now()
        });
    });
});
// Škálování: Redis adapter
const { createAdapter } = require('@socket.io/redis-adapter');
io.adapter(createAdapter(pubClient, subClient));

Summary

Socket.io simplifies implementaci. Pro scaling add Redis adapter a sticky sessions.

Need Help with Implementation?

Our team has experience designing and implementing modern architectures. We’re happy to help.

Free Consultation

Share:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.