{"id":13431868,"url":"https://github.com/kripod/wsx","last_synced_at":"2025-04-16T18:49:10.272Z","repository":{"id":57400133,"uuid":"64836366","full_name":"kripod/wsx","owner":"kripod","description":"Programmatically extensible lightweight WebSocket implementation in JavaScript.","archived":false,"fork":false,"pushed_at":"2016-08-22T10:57:04.000Z","size":1226,"stargazers_count":13,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-02T05:05:10.179Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kripod.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-03T10:14:16.000Z","updated_at":"2022-10-30T10:40:47.000Z","dependencies_parsed_at":"2022-09-05T03:01:13.388Z","dependency_job_id":null,"html_url":"https://github.com/kripod/wsx","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kripod%2Fwsx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kripod%2Fwsx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kripod%2Fwsx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kripod%2Fwsx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kripod","download_url":"https://codeload.github.com/kripod/wsx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249266491,"owners_count":21240765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T02:01:06.598Z","updated_at":"2025-04-16T18:49:10.255Z","avatar_url":"https://github.com/kripod.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# wsx\n\nProgrammatically extensible lightweight WebSocket implementation in JavaScript.\n\n[![Version (npm)](https://img.shields.io/npm/v/wsx.svg)](https://npmjs.com/package/wsx)\n[![Build Status](https://img.shields.io/travis/kripod/wsx/master.svg)](https://travis-ci.org/kripod/wsx)\n[![Code Coverage](https://img.shields.io/codecov/c/github/kripod/wsx/master.svg)](https://codecov.io/gh/kripod/wsx)\n[![Dependencies](https://img.shields.io/david/kripod/wsx.svg)](https://david-dm.org/kripod/wsx)\n[![Gitter](https://img.shields.io/gitter/room/kripod/wsx.svg)](https://gitter.im/kripod/wsx)\n\n## Introduction\n\nWSX provides a lightweight abstraction layer over WebSockets, greatly increasing\ndeveloper productivity while aiming to provide blazing fast transmission rates\nover the network without sacrificing stability and scalability.\n\n## Getting started\n\nPlease refer to the\n[API reference](https://kripod.github.io/wsx) and the\n[directory of examples](https://github.com/kripod/wsx/tree/master/examples) to\nlearn more about leveraging the possibilities within the library.\n\n### Initializing communication channels\n\n```js\nimport Client from 'wsx/client';\nimport Server from 'wsx/server';\n\nconst wsxServer = new Server({ port: 3000 });\nconst wsxClient = new Client('ws://localhost:3000');\n```\n\n_**Pro tip**: WSX tries to detect its environment automatically. This means that\n`import Server from 'wsx';` is a valid statement for Node environments, and\n`import Client from 'wsx';` is a valid statement for browser environments._\n\n### Sending and receiving messages\n\nWSX features an event-based messaging system with a syntax familiar for\n[Socket.IO](http://socket.io) users. Messages are serialized and deserialized\nautomatically.\n\n#### Example\n\n```js\n// Register an event handler on the server side for message type `echo`\nwsxServer.on('message:echo', (socket, payload) =\u003e {\n  socket.send('echo', payload);\n});\n\n// Register an event handler on the client side for message type `echo`\nwsxClient.on('message:echo', (payload) =\u003e {\n  console.log('Received message with type `echo` and the following payload:');\n  console.log(payload);\n});\n\n// Transmit a message with type `echo` to the server\nwsxClient.send('echo', 'Hello, World!');\n```\n\n#### Multiple recipients\n\nYou can broadcast messages or send them to socket groups.\n\n```js\nwsxServer.on('message', (socket, data) =\u003e {\n  // Forward the message to everyone else except for the socket that sent it\n  socket.broadcast(data);\n\n  // Forward the message to everyone, including the socket that sent it\n  wsxServer.sockets.send(data);\n\n  // Forward the message to a specific group of sockets\n  wsxServer.getSocketGroup('socketGroupId').send(data);\n});\n```\n\n### Socket groups\n\nSocket groups can be established on the server to handle multiple sockets with\nease. Their underlying `Set` of sockets is managed automatically, meaning that\nyou don't need to care about removing disconnected sockets.\n\n```js\nwsxServer.on('message:join', (socket, groupId) =\u003e {\n  // Inexistent socket groups are created automatically\n  wsxServer.getSocketGroup(groupId).add(socket);\n  socket.broadcast('join', groupId);\n});\n\nwsxServer.on('message:leave', (socket, groupId) =\u003e {\n  // Socket groups with zero sockets are destroyed automatically\n  wsxServer.getSocketGroup(groupId).remove(socket);\n  socket.broadcast('leave', groupId);\n});\n\nwsxClient.send('join', 'developers');\n```\n\n### Error handling\n\nMessage transmission errors are handled asynchronously:\n\n```js\nwsxServer.on('error', (error, socket) =\u003e {\n  // Server error\n  if (socket) {\n    // The error was caused by a socket\n  }\n});\n\nwsxClient.on('error', () =\u003e {\n  // Client error\n});\n```\n\n### Plugins\n\nPlugins can be used to extend the capability of WSX.\n\n```js\nconst wsxServerWithPlugins = new Server({\n  port: 3001,\n  plugins: [\n    (wsxServer) =\u003e {\n      // Modify the server instance\n    },\n  ],\n});\n\nconst wsxClientWithPlugins = new Client('ws://localhost:3001', {\n  plugins: [\n    (wsxClient) =\u003e {\n      // Modify the client instance\n    },\n  ],\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkripod%2Fwsx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkripod%2Fwsx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkripod%2Fwsx/lists"}