Skip to content

Commit

Permalink
docs(README): remove semicolons from examples and make session snippe…
Browse files Browse the repository at this point in the history
…t clearer
  • Loading branch information
MatthewWid committed Dec 9, 2024
1 parent 9acd617 commit 90ac754
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,58 +69,58 @@ Use [sessions](https://matthewwid.github.io/better-sse/reference/api/#sessionsta

```typescript
// Server
import { createSession } from "better-sse";
import { createSession } from "better-sse"

app.get("/sse", async (req, res) => {
const session = await createSession(req, res);

session.push("Hello world!");
});
const session = await createSession(req, res)
session.push("Hello world!", "message")
})
```

```typescript
// Client
const sse = new EventSource("/sse");
const eventSource = new EventSource("/sse")

sse.addEventListener("message", ({ data }) => {
console.log(JSON.parse(data));
});
eventSource.addEventListener("message", ({ data })) => {
const contents = JSON.parse(data)
console.log(contents) // Hello world!
})
```

Use [channels](https://matthewwid.github.io/better-sse/reference/api/#channelstate-sessionstate) to send events to many clients at once:

```typescript
import { createSession, createChannel } from "better-sse";
import { createSession, createChannel } from "better-sse"

const channel = createChannel();
const channel = createChannel()

app.get("/sse", async (req, res) => {
const session = await createSession(req, res);
const session = await createSession(req, res)

channel.register(session);
channel.register(session)

channel.broadcast("A user has joined.", "join-notification");
});
channel.broadcast("A user has joined.", "join-notification")
})
```

Loop over sync and async [iterables](https://matthewwid.github.io/better-sse/reference/api/#sessioniterate-iterable-iterable--asynciterable-options-object--promisevoid) and send each value as an event:

```typescript
const session = await createSession(req, res);
const session = await createSession(req, res)

const list = [1, 2, 3];
const list = [1, 2, 3]

await session.iterate(list);
await session.iterate(list)
```

Pipe [readable stream](https://matthewwid.github.io/better-sse/reference/api/#sessionstream-stream-readable-options-object--promiseboolean) data to the client as a stream of events:

```typescript
const session = await createSession(req, res);
const session = await createSession(req, res)

const stream = Readable.from([1, 2, 3]);
const stream = Readable.from([1, 2, 3])

await session.stream(stream);
await session.stream(stream)
```

---
Expand Down

0 comments on commit 90ac754

Please sign in to comment.