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 🙏

© 2024 – Pkg Stats / Ryan Hefner

deepexi_sse_utils

v0.0.8

Published

server send event

Downloads

29

Readme

deepexi_sse_utlis

与客户端建立长连接,解决需要需要轮询的困境。与webscoket不同的是,sse只是http协议,且只能有服务端发送通讯。

注意:node http连接默认2分钟会超时,如服务端连接发送事件的间隔需超过2分钟,需服务端自行处理(设置timeout时间 or 在2分钟的间隔内发送无效事件)

how to install

npm install deepexi_sse_utils

how to use

单次发送消息(如状态需实时返回给前端)

const SSEUtils = require('deepexi_sse_utils');
# server为服务端,监听请求
server.on('request', writeEventsOnce('hello world'));
function writeEventsOnce(msg) {
  return (req, res) => {
    const stream = SSEUtils.send({
      setResHeader: resHeaders => {
        res.writeHead(200, resHeaders);
      },
      sendType: 'once',
      onceMsg: msg,
    });

    res.body = stream.pipe(res);
  };
}

其他情况发送消息(非单次,或者其他任意情况)

const SSEUtils = require('deepexi_sse_utils');
# server为服务端,监听请求
server.on('request', writeEventsInterval('hello world', 1000));
function writeEventsInterval(msgs, times = 1000) {
  let interval = null;
  return (req, res) => {
    const stream = SSEUtils.send({
      setResHeader: resHeaders => {
        res.writeHead(200, resHeaders);
      },
      sendType: 'other',
      sender: send => {
        let index = 0;
        interval = setInterval(() => {
          const msg = index === msgs.length ? 'sseEnd' : msgs[index];
          send(msg);

          if (msg === 'sseEnd') {
            // 清除事件
            clearInterval(interval);
          }
          index++;
        }, times);
      },
    });

    res.body = stream.pipe(res);
  };
}

API

Methods

| 方法名 | 说明 | 参数 | | :--: | :--: | ---- | | send | 与客户端建立长连接,返回值是 stream对象 | optsions选项options.setResHeader 设置请求头function,requiredoptions.sendType once单次 repeat重复 other其他,默认发送一次options.sender 消息发送者,处理什么时候发送消息和结束发送消息,参数send func,结束需要发送'sseEnd'消息,非一次使用options.onceMsg 单次发送消息主体,默认是''options.retry 长连接发送错误时,重试频率,毫秒, 默认10s options.msgReplace 无法处理消息带换行符的情况,提供替换的正则,默认为'' options.finishCb stream结束时钩子函数 |

Events

| 事件名称 | 说明 | 参数 | | :--: | :--: | ---- | | sseEnd | 客户端接收到sseEnd事件,说明通讯已结束 | |