{"library":"sockjs-client","title":"SockJS Client","type":"library","description":"SockJS-client is a browser JavaScript library (current stable version 1.6.1) that provides a WebSocket-like object, abstracting away underlying transport mechanisms. It aims to offer a coherent, cross-browser, full-duplex communication channel between the browser and a web server, even in environments without native WebSocket support or behind restrictive corporate proxies. The library first attempts to use native WebSockets, falling back to various browser-specific transports (like streaming or polling) if necessary. Releases are somewhat irregular but active, with recent updates focusing on security fixes and dependency updates. A key differentiator is its robust fallback mechanism, adhering closely to the HTML5 WebSocket API, and supporting cross-domain connections without requiring Flash. It explicitly requires a server counterpart, such as `sockjs-node`, for functionality.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install sockjs-client"],"cli":null},"imports":["import SockJS from 'sockjs-client';","<script src=\"https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js\"></script>\n// SockJS is then available as a global variable","import SockJS from 'sockjs-client';\n// Or if type errors occur: import * as SockJS from 'sockjs-client';"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":"http://sockjs.org","github":"https://github.com/sockjs/sockjs-client","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/sockjs-client","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import SockJS from 'sockjs-client';\n\n// Ensure a SockJS server is running, e.g., on http://localhost:8080/my_websocket\n// For example, using sockjs-node:\n// const sockjs = require('sockjs');\n// const http = require('http');\n// const server = http.createServer();\n// const echo = sockjs.createServer();\n// echo.on('connection', (conn) => {\n//   conn.on('data', (message) => { conn.write(message); });\n//   conn.on('close', () => { console.log('Client disconnected from server'); });\n// });\n// echo.installHandlers(server, { prefix: '/my_websocket' });\n// server.listen(8080, '0.0.0.0', () => { console.log('SockJS server listening on 8080'); });\n\nconst socket = new SockJS('http://localhost:8080/my_websocket');\n\nsocket.onopen = () => {\n  console.log('SockJS connection opened');\n  socket.send('Hello from client!');\n};\n\nsocket.onmessage = (event) => {\n  console.log('Received message:', event.data);\n  // Optionally close the connection after receiving a message\n  // socket.close();\n};\n\nsocket.onclose = (event) => {\n  console.log('SockJS connection closed:', event.code, event.reason);\n  if (!event.wasClean) {\n    console.error('Connection abruptly disconnected.');\n    // Implement re-connection logic here\n  }\n};\n\nsocket.onerror = (error) => {\n  console.error('SockJS error:', error);\n};\n\n// Example: Send a message every 3 seconds\n// setInterval(() => {\n//   if (socket.readyState === SockJS.OPEN) {\n//     socket.send('Ping!');\n//   }\n// }, 3000);\n","lang":"typescript","description":"Demonstrates how to establish a connection with a SockJS server, send messages, and handle connection lifecycle events like opening, receiving messages, and closing. It also highlights the necessity of a server counterpart.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}