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

@micl/server-events

v0.1.8

Published

micl server events

Readme

@micl/server-events

npm version npm downloads license

一个轻量级的事件发布订阅模块,支持本地事件和跨服务 Redis 事件,适用于微服务架构中的事件驱动通信。

✨ 特性

  1. 支持本地事件发布订阅(基于 EventEmitter)
  2. 支持跨服务 Redis 事件发布订阅
  3. 单例模式,全局统一管理
  4. 完整的 TypeScript 类型支持
  5. 自动 JSON 序列化/反序列化
  6. 可配置的日志记录器

📦 安装

npm install @micl/server-events
# or
pnpm add @micl/server-events
# or
yarn add @micl/server-events

🎯 快速使用

配置文件

~/.server-events/redis.json 创建 Redis 配置文件:

{
  "url": "redis://:your_password@your_host:your_port"
}

JavaScript

const eventbus = require('@micl/server-events');

// 本地事件
eventbus.on('user:login', (data) => {
  console.log('User logged in:', data);
});
eventbus.emit('user:login', { userId: 123, name: 'Alice' });

// Redis 跨服务事件
eventbus.subscribe('order:created', (data) => {
  console.log('Order created:', data);
});
eventbus.publish('order:created', { orderId: 456, amount: 99.9 });

TypeScript

import eventbus from '@micl/server-events';

// 本地事件
eventbus.on('user:login', (data: { userId: number; name: string }) => {
  console.log('User logged in:', data);
});
eventbus.emit('user:login', { userId: 123, name: 'Alice' });

// Redis 跨服务事件
eventbus.subscribe(
  'order:created', 
  (data: { orderId: number; amount: number }) => {
    console.log('Order created:', data);
  }
);
eventbus.publish('order:created', { orderId: 456, amount: 99.9 });

📚 API 参考

方法

emit<T>(channel: string, message: T): void

触发本地事件,仅在当前进程内传播。

参数:

  • channel - 事件通道名称
  • message - 事件消息
eventbus.emit('user:login', { userId: 123 });

on<T>(channel: string, listener: (message: T) => void): void

注册本地事件监听器。

参数:

  • channel - 事件通道名称
  • listener - 事件处理函数
eventbus.on('user:login', (data) => {
  console.log('User logged in:', data);
});

publish<T>(channel: string, message: T): void

通过 Redis 发布跨服务事件。

参数:

  • channel - 事件通道名称
  • message - 事件消息(会自动 JSON 序列化)
eventbus.publish('order:created', { orderId: 456, amount: 99.9 });

subscribe<T>(channel: string, listener: (message: T) => void): void

通过 Redis 订阅跨服务事件。

参数:

  • channel - 事件通道名称
  • listener - 事件处理函数(消息会自动 JSON 反序列化)
eventbus.subscribe('order:created', (data) => {
  console.log('Order created:', data);
});

配置

options

全局配置对象。

| 配置项 | 类型 | 默认值 | 描述 | |--------|------|--------|------| | redisConfPath | string | '~/.server-events/redis.json' | Redis 配置文件路径 | | logger | object | undefined | 日志记录器,需包含 error/log/warn/info 方法 |

// 自定义日志记录器
eventbus.options.logger = {
  error: console.error,
  log: console.log,
  warn: console.warn,
  info: console.info,
};

// 自定义 Redis 配置路径
eventbus.options.redisConfPath = './config/redis.json';

logger

获取当前配置的日志记录器。

eventbus.options.logger = console;
console.log(eventbus.logger === console); // true

init(): void

重新初始化模块(通常在修改配置后调用)。

eventbus.options.redisConfPath = './new-path/redis.json';
eventbus.init();

🤝 贡献

欢迎提交 Issue 和 Pull Request 来完善这个模块。

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

Copyright (c) alexgogoing [email protected]

📞 支持

如有问题或建议,请提交 Issue 或联系维护者。