New versions were released under the next
release tag. Install it for new features and bug fixes.
With npm:
npm i @ghostebony/sse@next
With yarn:
yarn add @ghostebony/sse@next
With pnpm:
pnpm add @ghostebony/sse@next
@next docs
src/routes/+(page|layout).svelte
<script lang="ts">
import { onDestroy, onMount } from "svelte";
import { Client } from "@ghostebony/sse/client";
let eventSource: Client;
onMount(() => {
eventSource = new Client({
source: { url: "/sse" },
listeners: [
{
channel: "custom-channel",
listener: ({ data }) => {
console.log(data);
},
parseJson: true,
}
],
});
});
onDestroy(() => eventSource?.close());
</script>
src/routes/sse/+server.ts
import { ServerManager } from "@ghostebony/sse/server";
import type { RequestHandler } from "./$types";
const sse = ServerManager.addRoom("custom-room");
export const GET: RequestHandler = (event) =>
sse.server(event.getClientAddress() /* or user id */, {
connect: ({ user }) => {
// DO SOMETHING
},
disconnect: ({ user }) => {
// DO SOMETHING
},
});
somewhere on the server
import { ServerManager } from "@ghostebony/sse/server";
ServerManager.sendRoom(
"custom-room",
clientAddress /* or user id */,
1, // message id
"custom-channel", // channel that you're listening
data, // message data
);