{"id":17215424,"url":"https://github.com/pixtron/wss","last_synced_at":"2025-07-23T07:05:01.043Z","repository":{"id":50787201,"uuid":"206432915","full_name":"pixtron/wss","owner":"pixtron","description":"Simple pub/sub websocket server","archived":false,"fork":false,"pushed_at":"2021-05-29T14:40:40.000Z","size":6,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-14T08:40:10.025Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/pixtron.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-04T23:26:20.000Z","updated_at":"2019-09-05T14:02:19.000Z","dependencies_parsed_at":"2022-09-24T19:40:55.458Z","dependency_job_id":null,"html_url":"https://github.com/pixtron/wss","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pixtron/wss","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fwss","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fwss/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fwss/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fwss/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pixtron","download_url":"https://codeload.github.com/pixtron/wss/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fwss/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265300708,"owners_count":23743091,"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-10-15T03:24:22.934Z","updated_at":"2025-07-23T07:05:01.019Z","avatar_url":"https://github.com/pixtron.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @pxtrn/wss\n\nA aimple pub/sub websocket server\n\n## Installation\n\n`npm install --save @pxtrn/wss`\n\n## Usage\n\n### Basic example\n\n```js\nconst Wss = require('@pxtrn/wss');\n\nasync function createServer() {\n  const wss = new Wss({host: '127.0.0.1', port: 8081});\n  wss.publish('cats');\n  wss.publish('dogs');\n\n  await wss.connect();\n\n  return wss;\n}\n\ncreateServer().then(wss =\u003e {\n  let age = 0;\n  const name = 'kitty';\n\n  setInterval(function() {\n    // broadcasting a cat to the channel \"cats\"\n    wss.broadcast({age, name}, 'cats');\n    age++;\n  }, 1000);\n}).catch(err =\u003e {\n  console.error(err);\n});\n```\n\n### Subscribe and unsubscribe a client to a channel\n\nA more detailed example client, can be found in the [examples](examples)\n\n```js\nconst WebSocket = require('ws');\n\nconst client = new WebSocket('ws://localhost:8081');\n\nclient.on('open', () =\u003e {\n  //subscribe to channel \"cats\"\n  client.send(JSON.stringify({op: 'subscribe', channel: 'cats'}));\n\n  setTimeout(function() {\n    client.ping(function(){});\n  }, 5000);\n\n  setTimeout(function() {\n    //unsubscribe from channel \"cats\"\n    client.send(JSON.stringify({op: 'unsubscribe', channel: 'cats'}));\n  }, 3000);\n});\n\nclient.on('pong', function() {\n  console.log('got pong');\n});\n\nclient.on('message', message =\u003e {\n  console.log('got message', message);\n});\n```\n\n## API\n\n### Table of Contents\n\n- [Class: Wss](#class-wss)\n  - [new Wss(config[, logger])](#new-wssconfig-logger)\n  - [Event: 'close'](#event-close)\n  - [Event: 'connection'](#event-connection)\n  - [Event: 'error'](#event-error)\n  - [Event: 'headers'](#event-headers)\n  - [Event: 'listening'](#event-listening)\n  - [async server.connect()](#async-serverconnect)\n  - [async server.close()](#async-serverclose)\n  - [server.publish(channel)](#serverpublishchannel)\n  - [server.broadcast(channel)](#serverbroadcastdata-channel)\n\n### Class: Wss\n\nThis class represents a websocket server with simple pub/sub. It extends EventEmitter.\n\n#### new Wss(config[, logger])\n\n- `config` {Object}\n  [See options for `ws` modules `Class: WebSocket.Server`](https://github.com/websockets/ws/blob/HEAD/doc/ws.md#class-websocketserver)\n- `logger` {Object}\n\n  custom logger containing the following methods\n  ```js\n  const logger = {\n    debug: function(message, data) {}\n    notice: function(message, data) {}\n    info: function(message, data) {}\n    warning: function(message, data) {}\n    error: function(message, data) {},\n  }\n  ```\n\n#### Event: 'close'\n\nEmitted when the server closes.\n\n#### Event: 'connection'\n\n- `socket` {WebSocket}\n- `request` {http.IncomingMessage}\n\nEmitted when the handshake is complete. `request` is the http GET request sent\nby the client. Useful for parsing authority headers, cookie headers, and other\ninformation.\n\n#### Event: 'error'\n\n- `error` {Error}\n\nEmitted when an error occurs on the underlying server.\n\n#### Event: 'headers'\n\n- `headers` {Array}\n- `request` {http.IncomingMessage}\n\nEmitted before the response headers are written to the socket as part of the\nhandshake. This allows you to inspect/modify the headers before they are sent.\n\n#### Event: 'listening'\n\nEmitted when the underlying server has been bound.\n\n#### async server.connect()\n\nConnect the server\n\n#### async server.close()\n\nClose the server\n\n#### server.publish(channel)\n- `channel` {String}\n\nPublish a new `channel`. The same channel can only be published once\n\n#### server.broadcast(data[, channel])\n- `data` {Object |Array}\n- `channel` {String}\n\nBroadcast `data` to all clients in `channel`. If `channel` is undefined,\n`data` will be broadcastet to all connected clients.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpixtron%2Fwss","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpixtron%2Fwss","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpixtron%2Fwss/lists"}