@ghostebony/sse
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

@ghostebony/sse

⚠️ New releases

New versions were released under the next release tag. Install it for new features and bug fixes.

Installation

With npm:

npm i @ghostebony/sse@next

With yarn:

yarn add @ghostebony/sse@next

With pnpm:

pnpm add @ghostebony/sse@next

@next docs


v0.1.4 Examples

SvelteKit

client

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>

server

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
);

Package Sidebar

Install

npm i @ghostebony/sse

Weekly Downloads

7

Version

0.1.4

License

MIT

Unpacked Size

11.3 kB

Total Files

11

Last publish

Collaborators

  • ghostebony