{"library":"socket.io","title":"Socket.IO","type":"library","description":"Socket.IO is a real-time, bidirectional, event-based communication library that enables low-latency communication between web clients and Node.js servers. It facilitates cross-browser messaging with fallback options for reliable connections, even through proxies and firewalls. The current stable version is 4.8.3, and the package receives regular patch releases for bug fixes, security updates, and dependency maintenance across its ecosystem components.","language":"javascript","status":"active","last_verified":"Sat Apr 18","install":{"commands":["npm install socket.io"],"cli":null},"imports":["import { Server } from 'socket.io';"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":"https://socket.io","github":"https://github.com/socketio/socket.io","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/socket.io","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { Server } from 'socket.io';\nimport { createServer } from 'http';\n\nconst httpServer = createServer();\nconst io = new Server(httpServer, {\n  cors: {\n    origin: '*', // Allow all origins for simplicity in quickstart\n    methods: ['GET', 'POST']\n  }\n});\n\nio.on('connection', (socket) => {\n  console.log(`User connected: ${socket.id}`);\n\n  socket.emit('hello', `Welcome, ${socket.id}!`);\n\n  socket.on('message', (payload: string) => {\n    console.log(`Received message from ${socket.id}: ${payload}`);\n    // Broadcast the message to all connected clients\n    io.emit('broadcast', `Message from ${socket.id}: ${payload}`);\n  });\n\n  socket.on('disconnect', () => {\n    console.log(`User disconnected: ${socket.id}`);\n  });\n});\n\nconst PORT = process.env.PORT ?? 3000;\nhttpServer.listen(PORT, () => {\n  console.log(`Socket.IO server listening on port ${PORT}`);\n});","lang":"typescript","description":"This code sets up a basic Socket.IO server on port 3000 that listens for client connections. When a client connects, it logs the ID, sends a 'hello' event, listens for 'message' events from that client, and broadcasts them to all connected clients. It also handles client disconnections.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}