# express-sse-ts

> Strongly typed server sent events module for express

Latest version **1.0.2** (published 2020-09-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-sse-ts
pnpm add express-sse-ts
yarn add express-sse-ts
bun add express-sse-ts
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-09-08 |
| First published | 2020-09-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Merijn |
| Maintainers | dubbelster |
| Keywords | express, sse, server, sent, events, typescript, ts, middleware, eventsource |

## Links

- npm: https://www.npmjs.com/package/express-sse-ts
- Repository: https://github.com/dubbelster/express-sse-ts
- Homepage: https://github.com/dubbelster/express-sse-ts#readme
- Issues: https://github.com/dubbelster/express-sse-ts/issues
- npm.io page: https://npm.io/package/express-sse-ts

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-09-08
- 1.0.1 — 2020-09-07
- 1.0.0 — 2020-09-07
- 0.0.5 — 2020-09-07
- 0.0.4 — 2020-09-07
- 0.0.3 — 2020-09-07
- 0.0.2 — 2020-09-06
- 0.0.1 — 2020-09-06
- 0.0.0 — 2020-09-06

## README

# express-sse-ts

Strongly typed server sent events module for express.
This modules is used as express middleware.

Because the module is strongly typed you'll have usefull helpers while coding!
This does not mean you have to use TypeScript for it to work.
Both TypeScript and JavaScript will work!

![Screenshot of usefull helper!](https://raw.githubusercontent.com/dubbelster/express-sse-ts/master/README%20assets/codingExample.png)

I am happy to help if you encounter any problems!

## Installation

```
npm i express-sse-ts
```


## Usage

If you want to see a more extensive example, look at the [example source code](https://github.com/dubbelster/express-sse-ts-example)

### Server
```javascript
import SSE from "express-sse-ts";
import express from "express"
import * as path from "path";

const app = express();
const sse = new SSE(); // Create SSE instance.

// Send HTML page to client
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'sandbox.html'));
})

// '/events' is the URL for the EventSource in the client.
app.get('/events', sse.init); // The init() function sets up the connection between the server and the client

// Send a message every second to the clients using SSE!
let counter = 0;
setInterval(() => {
    // Sends message to all connected clients!
    sse.send(`Message #${counter++}`);

    // All options for sending a message:
    sse.send('data: string', 'eventName?: string', 'id?: string | number | undefined')
}, 1000);

// Spin up the server!
app.listen(3000, () => {
    console.log('Express app listening on http://localhost:3000.')
});
```

### Client
```javascript
const display = document.getElementById('display');
const es = new EventSource('/events'); // Create EventSource

// Listen to event with name 'message'
es.onmessage = event => {
    // Do something width event.
}

// Listen to event with name 'eventName'
es.addEventListener('eventName', event => {
    // Do something width event.
});
```

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