# deepexi_sse_utils

> server send event

Latest version **0.0.8** (published 2019-08-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install deepexi_sse_utils
pnpm add deepexi_sse_utils
yarn add deepexi_sse_utils
bun add deepexi_sse_utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2019-08-07 |
| First published | 2019-06-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 64.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | qiye |
| Maintainers | lujunwei |
| Keywords | sse |

## Links

- npm: https://www.npmjs.com/package/deepexi_sse_utils
- Repository: https://github.com/lujunwei/sse_utlis
- Homepage: https://github.com/lujunwei/sse_utlis#readme
- Issues: https://github.com/lujunwei/sse_utlis/issues
- npm.io page: https://npm.io/package/deepexi_sse_utils

## Recent versions

- 0.0.8 (latest) — 2019-08-07
- 0.0.7 — 2019-08-07
- 0.0.6 — 2019-08-06
- 0.0.5 — 2019-07-18
- 0.0.4 — 2019-07-17
- 0.0.3 — 2019-06-24
- 0.0.2 — 2019-06-24
- 0.0.1 — 2019-06-24

## README

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

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

## how to install

```sh
npm install deepexi_sse_utils
```

## how to use

### 单次发送消息（如状态需实时返回给前端）
```js
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);
  };
}
```
### 其他情况发送消息（非单次，或者其他任意情况）
```js
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选项<br />options.setResHeader 设置请求头function，required<br />options.sendType once单次 repeat重复 other其他，默认发送一次<br />options.sender 消息发送者，处理什么时候发送消息和结束发送消息，参数send func，结束需要发送'sseEnd'消息，非一次使用<br />options.onceMsg 单次发送消息主体，默认是''<br />options.retry 长连接发送错误时，重试频率，毫秒, 默认10s<br/> options.msgReplace 无法处理消息带换行符的情况，提供替换的正则，默认为''<br /> options.finishCb stream结束时钩子函数 |
### Events
|   事件名称   | 说明 | 参数 |
| :--: | :--: | ---- |
| sseEnd | 客户端接收到sseEnd事件，说明通讯已结束 |  |

---
_Source: https://npm.io/package/deepexi_sse_utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
