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

@mingto/mt-sse

v1.0.28

Published

明途SSE请求工具

Readme

@mingto/mt-sse

用于处理服务器发送事件(Server-Sent Events,SSE)的 TypeScript 库,提供简单易用的接口来管理和监听来自服务器的事件。

特性

  • 🔄 事件驱动:支持 startmessageenderrorclose 等事件
  • 📡 自动重连:内置连接管理机制
  • 🔧 灵活配置:支持自定义请求头和请求方法
  • 🎯 类型安全:完整的 TypeScript 类型定义

安装

pnpm add @mingto/mt-sse

快速开始

import SSEClient from '@mingto/mt-sse'

const client = new SSEClient('https://yourserver.com/sse')

client.on('start', (response) => {
  console.log('SSE 连接已建立', response)
})

client.on('message', (data) => {
  console.log('收到消息:', data)
})

client.on('end', (data) => {
  console.log('连接结束:', data)
})

client.on('error', (error) => {
  console.error('SSE 错误:', error.message)
})

client.on('close', () => {
  console.log('SSE 连接已关闭')
})

// 发送消息
client.send({ key: 'value' })

// 关闭连接
client.closeSSE()

配置选项

const options = {
  headers: {
    'cache-control': 'no-cache',
    'Connection': 'keep-alive',
    'content-type': 'application/json',
    'Language': 'zh'
  },
  method: 'POST'
}

const client = new SSEClient('https://yourserver.com/sse', options)

| 配置项 | 说明 | 默认值 | |--------|------|--------| | headers | 请求头 | cache-control: no-cache, Connection: keep-alive, content-type: application/json, Language: zh | | method | 请求方法 | POST |

事件监听

| 事件名 | 说明 | 回调参数 | |--------|------|----------| | start | SSE 连接成功建立时触发 | response | | message | 接收到消息时触发 | data | | end | 接收到结束标记时触发(状态码 114) | data | | error | 发生错误时触发 | SSEError | | close | 连接正常关闭时触发 | 无 |

client.off(eventName, callback?)

移除事件监听。如果不传回调,则移除该事件的所有监听。

// 移除指定回调
client.off('message', messageCallback)

// 移除该事件的所有回调
client.off('message')

// 移除所有事件的所有回调
client.off()

状态码

| 状态码 | 说明 | |--------|------| | 112 | 正常消息 | | 100 | 用户被禁用 | | 114 | 连接结束 |

错误处理

| 错误代码 | 说明 | |----------|------| | 4001 | 浏览器不支持 SSE | | 4002 | SSE 执行错误 | | 4003 | SSE 连接错误 | | 4004 | 未知错误 | | 4005 | 用户被停用 |

client.on('error', (error) => {
  if (error.code === '4001') {
    console.error('您的浏览器不支持 SSE')
  } else if (error.code === '4003') {
    console.error('连接服务器失败,请检查网络')
  }
})

完整示例

import SSEClient from '@mingto/mt-sse'

const client = new SSEClient('https://yourserver.com/sse', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your-token'
  }
})

// 监听消息
client.on('message', (data) => {
  if (data.type === 'NORMAL') {
    // 处理业务数据
    console.log('收到数据:', data.content)
  }
})

// 监听结束
client.on('end', () => {
  console.log('数据接收完成')
})

// 发送请求参数
client.send({
  message: 'Hello',
  sessionId: '12345'
})

许可证

MIT