{"id":17446008,"url":"https://github.com/jamen/ws-utils","last_synced_at":"2025-07-23T12:34:40.786Z","repository":{"id":96785565,"uuid":"166340969","full_name":"jamen/ws-utils","owner":"jamen","description":"Functions for using WebSockets on Node.js.","archived":false,"fork":false,"pushed_at":"2019-05-17T20:36:11.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-02T11:24:11.879Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamen.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-18T03:57:32.000Z","updated_at":"2021-12-19T15:25:50.000Z","dependencies_parsed_at":"2023-07-14T09:02:05.374Z","dependency_job_id":null,"html_url":"https://github.com/jamen/ws-utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jamen/ws-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamen%2Fws-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamen%2Fws-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamen%2Fws-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamen%2Fws-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamen","download_url":"https://codeload.github.com/jamen/ws-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamen%2Fws-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266680294,"owners_count":23967791,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-17T18:20:16.081Z","updated_at":"2025-07-23T12:34:40.758Z","avatar_url":"https://github.com/jamen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jamen/ws-extras\n\nFunctions for using WebSockets on Node.js.\n\nThis package is inpsired after [`micro`](https://github.com/zeit/micro), except made for WebSockets instead of HTTP. Its designed for use with [`ws`](https://github.com/websockets/ws).\n\n## Usage\n\n### `loop(conn, recv)`\n\nA loop for receiving messages from the connection through `recv`. Once the connection closes the loop completes.\n\n```js\nconst WebSocket = require('ws')\nconst { loop } = require('@jamen/ws-extras')\n\nconst server = new WebSocket.Server({ host, port })\n\nserver.on('connection', conn =\u003e loop(conn, recv))\n\nasync function recv (conn) {\n    // Receive messagess\n}\n```\n\nTo get messages from `conn` in this loop, see the functions below.\n\n### `buffer(conn, options)`\n\n### `text(conn, options)`\n\n### `json(conn, options)`\n\nReceive the next buffer, text, or JSON message.\n\nThe `options` is an object that can contain\n\n- `limit`: A [byte amount](https://github.com/visionmedia/bytes.js#readme) of how large the message can be. Defaults to `'1mb'`.\n- `timeout`: A [duration](https://github.com/zeit/ms) to stop waiting for the message. When `false` it never stops. Defaults to `'1m'`.\n- `encoding`: The preferred encoding. Defaults to `'utf8'.\n\nThe function returns a promise resolving the result. The promise can reject with an error if the limit is reached, the timeout is reached, or an error from `ws` emerges.\n\n```js\nconst { buffer, text, json } = require('@jamen/ws-extras')\n\nasync function recv (conn) {\n    let message\n    // Next message as a buffer\n    message = await buffer(conn)\n    // As text\n    message = await text(conn, { limit: '5mb', timeout: '30s' })\n    // As JSON\n    message = await json(conn, { limit: 2097152, timeout: 60000 })\n    // Without timeout\n    message = await json(conn, { timeout: false })\n}\n```\n\n### `jsonEvent(conn, options)`\n\nSimilar to `json` except only `{ event, data }` is allowed. The `options` are the same.\n\nThe returned promise can be rejected if the message is not a `{ event, data }` structure.\n\n```js\nconst { jsonEvent } = require('@jamen/ws-extras')\n\nasync function recv (conn) {\n    const { event, data } = await jsonEvent(conn)\n\n    // ...\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamen%2Fws-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamen%2Fws-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamen%2Fws-utils/lists"}