{"id":13672811,"url":"https://github.com/flarestart/egg-websocket-plugin","last_synced_at":"2025-09-03T20:13:29.728Z","repository":{"id":50451067,"uuid":"231189408","full_name":"flarestart/egg-websocket-plugin","owner":"flarestart","description":"A standard WebSocket plugin for egg.js","archived":false,"fork":false,"pushed_at":"2023-04-26T15:09:05.000Z","size":318,"stargazers_count":53,"open_issues_count":7,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-23T19:14:42.360Z","etag":null,"topics":["egg","egg-websocket","plugin","websocket","ws"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/flarestart.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-01-01T07:51:47.000Z","updated_at":"2024-08-15T07:11:05.000Z","dependencies_parsed_at":"2024-01-17T04:18:42.755Z","dependency_job_id":null,"html_url":"https://github.com/flarestart/egg-websocket-plugin","commit_stats":{"total_commits":19,"total_committers":2,"mean_commits":9.5,"dds":"0.052631578947368474","last_synced_commit":"aa84998cbe284a98e38499162fae5eb0e30139c5"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/flarestart/egg-websocket-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flarestart%2Fegg-websocket-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flarestart%2Fegg-websocket-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flarestart%2Fegg-websocket-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flarestart%2Fegg-websocket-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flarestart","download_url":"https://codeload.github.com/flarestart/egg-websocket-plugin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flarestart%2Fegg-websocket-plugin/sbom","scorecard":{"id":402610,"data":{"date":"2025-08-11","repo":{"name":"github.com/flarestart/egg-websocket-plugin","commit":"aa84998cbe284a98e38499162fae5eb0e30139c5"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/21 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 4 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"31 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-75v8-2h7p-7m2m","Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22","Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp","Warn: Project is vulnerable to: GHSA-593f-38f6-jp5m","Warn: Project is vulnerable to: GHSA-x2rg-q646-7m2v","Warn: Project is vulnerable to: GHSA-jgmv-j7ww-jx2x","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55","Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-76p7-773f-r4q5","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-wqq4-5wpv-mx2g","Warn: Project is vulnerable to: GHSA-3787-6prv-h9w3","Warn: Project is vulnerable to: GHSA-9qxr-qj54-h672","Warn: Project is vulnerable to: GHSA-m4v8-wqvr-p9f7","Warn: Project is vulnerable to: GHSA-c76h-2ccp-4975","Warn: Project is vulnerable to: GHSA-cxrh-j4jr-qwg3","Warn: Project is vulnerable to: GHSA-p5gc-c584-jj6v","Warn: Project is vulnerable to: GHSA-whpj-8f3w-67p5","Warn: Project is vulnerable to: GHSA-cchq-frgv-rjh5","Warn: Project is vulnerable to: GHSA-g644-9gfx-q4q4","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-18T20:25:17.785Z","repository_id":50451067,"created_at":"2025-08-18T20:25:17.785Z","updated_at":"2025-08-18T20:25:17.785Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273502349,"owners_count":25117172,"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-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["egg","egg-websocket","plugin","websocket","ws"],"created_at":"2024-08-02T09:01:49.454Z","updated_at":"2025-09-03T20:13:29.701Z","avatar_url":"https://github.com/flarestart.png","language":"TypeScript","readme":"# egg-websocket-plugin\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/egg-websocket-plugin.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-websocket-plugin\n[travis-image]: https://img.shields.io/travis/flarestart/egg-websocket-plugin.svg?style=flat-square\n[travis-url]: https://travis-ci.org/flarestart/egg-websocket-plugin\n[codecov-image]: https://img.shields.io/codecov/c/github/flarestart/egg-websocket-plugin.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/flarestart/egg-websocket-plugin?branch=master\n[david-image]: https://img.shields.io/david/flarestart/egg-websocket-plugin.svg?style=flat-square\n[david-url]: https://david-dm.org/flarestart/egg-websocket-plugin\n[snyk-image]: https://snyk.io/test/npm/egg-websocket-plugin/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/egg-websocket-plugin\n[download-image]: https://img.shields.io/npm/dm/egg-websocket-plugin.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-websocket-plugin\n\n\u003c!--\nDescription here.\n--\u003e\n\n## 依赖说明\n\n### 依赖的 egg 版本\n\n✅ egg 2.x 安装 egg-websocket-plugin@1.0.1\n✅ egg 3.x 安装 egg-websocket-plugin@3.0.0+\n\n\n\n## 特性支持\n\n- [x] 兼容 egg 自身的 controller\n- [x] 兼容 egg 路由中间件\n- [x] 不需要像 socket.io 一样开启 `sticky` 模式\n- [x] 支持消息广播订阅\n\n\n\n## 使用方式\n\n```bash\nyarn add egg-websocket-plugin\n```\n\n### 1. 开启插件\n\n```js\n// config/plugin.js\nexports.websocket = {\n  enable: true,\n  package: 'egg-websocket-plugin',\n};\n```\n\n### 2. 配置 WebSocket 路由\n\n```js\n// app/router.js\napp.ws.route('/ws', app.controller.home.hello);\n\n// 可使用路由参数，使用方式同 egg 自身路由\napp.ws.route('/foo/:id', app.controller.home.foo);\n```\n\n### 3. 配置 WebSocket 全局中间件【可选】\n**注意**：配置 WebSocket 全局中间件应当在所有 `app.ws.route` 注册路由之前进行，否则之前的路由不会应用到全局中间件，且插件会生成一条警告日志\n\n```js\n// app/router.js\n\n// 配置 WebSocket 全局中间件\napp.ws.use(async (ctx, next) =\u003e {\n  console.log('websocket open');\n  await next();\n  console.log('websocket closed');\n});\n```\n\n### 4. 配置路由中间件【可选】\n\n**路由会依次用到 app.use, app.ws.use, 以及 app.ws.router 中配置的中间件**\n\n```js\n// app/router.js\nasync function middleware(ctx, next) {\n  console.log('open', ctx.starttime);\n  await next();\n}\n// 配置路由中间件\napp.ws.route('/ws', middleware, app.controller.home.hello);\n```\n\n### 5. 禁用 App 全局中间件\n\n\u003e 插件默认会使用 app.use(...) 注册的中间件，如果你不想使用它们，或者这些插件与 websocket 有冲突，你可以在 `config.default.js` 中禁用它们\n\n```js\nconfig.websocket = {\n  useAppMiddlewares: false,\n};\n```\n\n### 6. 在控制器中使用 websocket\n\nwebsocket 是一个 `ws` 插件的实例，可阅读 [ws](https://www.npmjs.com/package/ws) 插件的说明文档或 TypeScript 的定义\n\n```js\n// app/controller/home.js\nimport { Controller } from 'egg';\n\nexport default class HomeController extends Controller {\n  async hello() {\n    const { ctx, app } = this;\n    if (!ctx.websocket) {\n      throw new Error('this function can only be use in websocket router');\n    }\n\n    console.log('client connected');\n\n    ctx.websocket\n      .on('message', (msg) =\u003e {\n        console.log('receive', msg);\n      })\n      .on('close', (code, reason) =\u003e {\n        console.log('websocket closed', code, reason);\n      });\n  }\n}\n\n```\n\n\n\n## 支持 Redis 消息发布订阅【可选】\n\n### 开启消息广播\n\n**注意**：`egg-websocket-plugin` 依赖于 `ioredis` 但可与其他插件如 `egg-redis` 共用一个 `ioredis` 依赖\n\n开启广播相关的 API 需要在 config.default.js 中配置 `redis` 连接，配置项可参见 [ioredis](https://www.npmjs.com/package/ioredis)\n\n```typescript\nconfig.websocket = {\n  // 配置 websocket 使用 redis 作消息广播，配置项目参见 ioredis\n  redis: {\n    host: '127.0.0.1',\n    port: 6379,\n  },\n};\n```\n\n### 房间消息广播 API\n\n\u003e 开启 redis 配置后，即可使用下面的广播 API\n\n#### 1. app.ws.sendTo(room, data)\n\n\u003e 发送消息到某个房间，将 data 原样发送到客户端连接\n\n- `room` \u0026lt;string\u0026gt;: 房间名\n- `data` \u0026lt;string | Buffer\u0026gt;: 发送内容，原样发送\n\n#### 2. app.ws.sendJsonTo(room, data)\n\n\u003e 发送消息到某个房间，会自动使用 JSON.stringify 对 data 进行序列化\n\n- `room` \u0026lt;string\u0026gt; 房间名\n- `data` \u0026lt;any\u0026gt; 发送内容，可进行 JSON 序列化的任意对象\n\n#### 3. ctx.websocket.room.join(room, fn?)\n\u003e 将当前连接加入房间\n\n- `room` \u0026lt;string\u0026gt; 要加入的房间名\n- `fn` \u0026lt;function({room, message})\u0026gt; 可选参数，用于配置房间消息处理函数，配置详情见 [自定义房间消息处理函数](#roomhandler)\n  - `room` \u0026lt;string\u0026gt; 消息所属房间名\n  - `message`\u003cstring | Buffer\u003e 房间内的广播消息，消息类型取决于 `sendTo()` 发送时的类型\n  - 忽略此参数，则该房间的消息将直接发送到客户端连接\n  - 配置这个参数，则可以手动处理消息发送逻辑，不再自动将房间内的消息发送到客户端连接\n\n#### 4. ctx.websocket.room.leave(room)\n\u003e 当前连接离开房间，当前连接关闭后，将自动退出当前连接加入过的房间，因此关闭连接前无需要手动离开房间\n\n- `room` \u0026lt;string\u0026gt; 房间名\n\n#### 5. ctx.websocket.room.sendTo(room, data)\n\n\u003e 发送消息到某个房间，将 data 原样发送到客户端连接，功能与 app.ws.sendTo 相同\n\n#### 6. ctx.websocket.room.sendJsonTo(room, data)\n\n\u003e 发送消息到某个房间，会自动使用 JSON.stringify 对 data 进行序列化，功能与 `app.ws.sendTo` 相同\n\n\n\n```typescript\n// 当前连接加入房间 foo，收到房间的消息将直接发送到当前客户端连接\nctx.websocket.room.join('foo');\n\n// 当前连接加入房间 bar，并设置该房间消息处理函数，设置了处理函数将不再直接发送消息到当前客户端\n// room: string, message: Buffer | string\nctx.websocket.room.join('bar', ({ room, message }) =\u003e {\n  console.log(room, message);\n});\n\n// 当前连接离开房间\nctx.websocket.room.leave('foo');\n\n// 通过当前 ctx 广播消息，所有加入房间的连接会收到这条消息\nctx.websocket.room.sendTo('foo', 'hello foo');\n\n// 也可以通过通过 app 对象来广播消息\napp.ws.sendTo('bar', 'hello bar');\n```\n\n\n\n\n\n### 自定义房间消息处理函数\u003ca id=\"roomhandler\"\u003e\u003c/a\u003e\n\n**注意**：使用 `room.join()` 函数，忽略第二个参数的情况下，房间内的消息会直接发送到当前客户端连接。\n\n如果你需要自定义这部分的处理逻辑，那么可配置 `room.join()` 的第二个参数。\n\n```typescript\nctx.websocket.room.join('foo', ({ room, message }) =\u003e {\n  // room: string 房间名\n  // message: Buffer | string 消息内容\n  // message 的类型取决于使用 sendTo 发送的是 string 还是 Buffer\n  console.log(room, message);\n});\n```\n\n\n\n### 例子\n\nrouter.ts\n\n```typescript\nimport { Application } from 'egg';\n\nexport default (app: Application) =\u003e {\n  const { controller, router } = app;\n\n  // 配置 http 路由\n  router.get('/', controller.home.index);\n  // 配置 websocket 路由，websocket 可与 http 使用同一个 url，不会导致冲突\n  app.ws.route('/', controller.home.ws);\n};\n```\n\ncontroller/home.ts\n\n```typescript\nimport { Controller } from 'egg';\n\nexport default class HomeController extends Controller {\n  public async index() {\n    const { ctx, app } = this;\n    // 向房间 foo 发送消息\n    app.ws.sendTo('foo', 'hello from index');\n    ctx.body = 'index';\n  }\n\n  public async ws() {\n    const { websocket } = this.ctx;\n    if (!websocket) {\n      throw new Error('this function can only be use in websocket router');\n    }\n\n    // 加入到房间 foo\n    websocket.room.join('foo');\n  }\n}\n```\n\n浏览器连接 WebSocket\n\n```javascript\nconst ws = new WebSocket('ws://127.0.0.1:7001/');\nws.addEventListener('message', data =\u003e {\n  console.log(data);\n});\n```\n\n这时候，访问 http://localhost:7001/ WebSocket 客户端将会收到一条  `hello from index` 的字符串\n\n\n\n## 使用场景\n\n- 使用 egg 开发标准 WebSocket 服务器\n- 微信小程序 WebSocket 服务器\n\n\n\n## 提问交流\n\n请到 [issues](https://github.com/flarestart/egg-websocket-plugin/issues) 异步交流。\n\n\n\n## License\n\n[MIT](LICENSE)\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflarestart%2Fegg-websocket-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflarestart%2Fegg-websocket-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflarestart%2Fegg-websocket-plugin/lists"}