{"library":"socket.io-client","title":"Socket.IO Client","type":"library","description":"Socket.IO Client is a JavaScript library designed for facilitating real-time, bidirectional, and event-based communication between web clients (browsers or Node.js applications) and a Socket.IO server. It abstracts away the complexities of WebSocket and HTTP long-polling, providing automatic fallback mechanisms, robust reconnection logic, and packet buffering to ensure reliable connectivity even in unstable network conditions. The current stable version, 4.8.3, is part of the actively developed Socket.IO v4 ecosystem. Releases are typically aligned with the server-side `socket.io` package, focusing on bug fixes, performance improvements, and dependency updates. Its key differentiators include its focus on cross-platform compatibility, simplified API for event handling, and built-in reliability features that make it a go-to choice for real-time applications requiring persistent connections.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install socket.io-client"],"cli":null},"imports":["import { io } from 'socket.io-client';","import type { Socket } from 'socket.io-client';","import { Manager } from 'socket.io-client';"],"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-client","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { io, Socket } from 'socket.io-client';\n\n// Connect to a Socket.IO server running on localhost:3000\nconst socket: Socket = io('http://localhost:3000');\n\n// Event listener for successful connection\nsocket.on('connect', () => {\n  console.log(`Connected to the server with ID: ${socket.id}`);\n  // Emit a 'greeting' event to the server\n  socket.emit('greeting', 'Hello from the client!');\n});\n\n// Event listener for incoming 'message' events from the server\nsocket.on('message', (data: string) => {\n  console.log('Received message:', data);\n});\n\n// Event listener for disconnection\nsocket.on('disconnect', (reason: Socket.DisconnectReason) => {\n  console.log(`Disconnected from the server: ${reason}`);\n});\n\n// Event listener for connection errors\nsocket.on('connect_error', (error: Error) => {\n  console.error('Connection error:', error.message);\n});\n\n// Manually disconnect after 15 seconds (optional)\nsetTimeout(() => {\n  if (socket.connected) {\n    console.log('Manually disconnecting after 15 seconds...');\n    socket.disconnect();\n  }\n}, 15000);","lang":"typescript","description":"Establishes a connection to a Socket.IO server, sends a message upon connection, listens for incoming messages, and handles connection lifecycle events like disconnects and errors.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}