npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@diyaner/egg-ws

v4.7.3

Published

egg plugin ws package for websocket.

Readme

egg-ws

v4.0 非兼容性更新

  1. worker 的 onMessage 对象的属性 data 变更为 msg。data 变为接收 msg.data 的值
  2. worker 业务逻辑入口监听事件的回调函数第一个为 onMessage 实例不变,第二个参数由 msg 变更为 msg.data
  3. 封装客户端。
  4. unit test 完善。
  5. 重构提取一层 common 逻辑,封装 onMessage 等作为 adapter。在 worker 和 agent 的接口实现处扩各自的 adapter 实现接口即可。

Install

$ npm i diyaner/egg-ws --save

Usage

// {app_root}/config/plugin.js
exports.ws = {
  enable: true,
  package: "@diyaner/egg-ws",
};

Configuration

// {app_root}/config/config.default.js
exports.ws = {
  // 传入ws模块的配置参数。可参考ws模块文档。
  wsOptions: {
    port: 7002,
  },
  // egg-ws插件加载时是否立即实例化web socket,默认true。若配置为false,则在应用中应该在didReady钩子中手动执行ws.init()方法进行初始化。
  immediate: true,
};

see config/config.default.js for more detail.

Example

在 app worker 使用

// app.js中执行init。然后写业务逻辑。
const wss = this.app.ws.init();
// app.js 中进行ws的业务逻辑事件监听
wss.on("event", (ws, data) => {
  //回调内可调用send向客户端回复消息
  ws.send();
});

// 在controller、service等业务逻辑处
// 推送消息(发送)
this.app.ws.emit("event", wsuid, data);
// 推送给全部客户端
this.app.ws.emit("event-name", "*", data);
// 推送给某些客户端
this.app.ws.emit("event-name", [wsuid1, wsuid2], data);

// 监听前端消息(接收)
this.app.ws.on("event", (ws, data) => {});
// 内置事件。新client连接事件。内置的事件,自定义事件时不能用这个名字。否则会发生错乱。
this.app.ws.on("ws-connection", (ws, data) => {});

详细实例可查看app.ts

在 agent 进程使用

插件已内置启动了 server。并把 server 实例挂载在 agent 的静态属性上。应用层的 agent 中直接使用 agent.wss

if (agent.wss) {
  // 接收前端消息事件。
  agent.wss.work.on("abc", (data: IPCData) => {
    console.log(process.pid);
    console.log(data);
    data.data = { is: "agent" };
    // 发送消息给前端。
    agent.wss?.work.emit("ws-response", data);
  });
}

详细实例可查看agent.ts

此处的发送消息事件详细可查阅./lib/agent/workerInterface.ts中的监听事件。且 data 必须符合 IPCData 接口定义。

// IPCData 定义在ws.d.ts中:
interface IPCData {
  wsuids: string[];
  wsuid: string;
  event: string;
  randId?: string;
  data: any;
}

注意:agent 模式和 worker 模式是提供的两种业务逻辑接口,不能同时监听同一个业务事件和处理返回,否则会多次执行逻辑。另外,由于 agent 进程必须保持稳定性,且与 worker 普通业务逻辑分隔,无法与普通业务交互,所以通常的应用场景是 agent 上处理简单的,不依赖普通 http 业务的 ws 业务。如一些很简单的 ws 在线 user list 信息推送等。而比较重的业务交互逻辑还是发送给 worker 处理。这其中又由于 agent 和 worker 进程通讯会消耗资源,因此各有优劣,处理业务时应该因地制宜合适地应用 agent 和 worker 两种方式。

Questions & Suggestions

Please open an issue here.

License

MIT