{"library":"socketcluster-client","title":"SocketCluster Client","type":"library","description":"SocketCluster Client (socketcluster-client) is the JavaScript client library for connecting to SocketCluster servers, enabling high-performance, real-time, bi-directional communication over WebSockets. It provides abstractions for pub/sub (channels), remote procedure calls (RPC), and efficient data streaming. The library is currently at version 20.0.1, indicating an active development cycle with frequent major releases that often introduce breaking changes. Key differentiators include its focus on scalability, built-in support for backpressure handling, and a clear API for consuming events and data streams using async iterators, making it suitable for demanding real-time applications like chat, gaming, and financial dashboards. It is designed to work seamlessly with `socketcluster-server`.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install socketcluster-client"],"cli":null},"imports":["import { create } from 'socketcluster-client';","import { SCClientSocket } from 'socketcluster-client';","const socketClusterClient = require('socketcluster-client'); // CommonJS\n// Or in browser after script tag:\nconst socket = socketClusterClient.create(...);"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":"https://socketcluster.io","github":"https://github.com/SocketCluster/socketcluster-client","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/socketcluster-client","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { create } from 'socketcluster-client';\n\nconst socket = create({\n  hostname: 'localhost',\n  port: 8000,\n  autoConnect: true,\n  autoReconnectOptions: {\n    initialDelay: 1000,\n    randomness: 500,\n    multiplier: 1.2,\n    maxDelay: 10000\n  }\n});\n\n(async () => {\n  try {\n    await socket.listener('connect').once();\n    console.log('Socket connected successfully to server.');\n\n    // Transmit data to the server without expecting a response\n    socket.transmit('chatMessage', { user: 'developer', text: 'Hello SocketCluster!' });\n\n    // Invoke a remote procedure call (RPC) and await a response\n    const serverTime = await socket.invoke('getServerTime');\n    console.log('Server time received:', serverTime);\n\n    // Subscribe to a channel and consume messages\n    const myChannel = socket.subscribe('myPublicChannel');\n    await myChannel.listener('subscribe').once();\n    console.log('Successfully subscribed to channel: myPublicChannel');\n\n    // Publish a message to the channel\n    await myChannel.invokePublish('This is a message from the client.');\n    console.log('Published message to channel.');\n\n    // Consume messages from the channel using an async iterator\n    for await (const data of myChannel) {\n      console.log('Received channel message:', data);\n      // For demonstration, process one message then unsubscribe\n      break;\n    }\n    myChannel.unsubscribe();\n    console.log('Unsubscribed from channel.');\n\n  } catch (error) {\n    console.error('Socket error or connection failed:', error);\n  }\n\n  // Disconnect the socket after a short delay\n  setTimeout(() => {\n    socket.disconnect();\n    console.log('Socket disconnected.');\n  }, 5000);\n})();","lang":"typescript","description":"Demonstrates connecting to a SocketCluster server, transmitting data, invoking an RPC, subscribing to a channel, publishing to it, and consuming messages using async iterators.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}