x/oak/server_sent_event.ts

A middleware framework for handling HTTP with Deno 🐿️ 🦕
Very Popular
Go to Latest
interface ServerSentEventTarget
implements EventTarget
import { type ServerSentEventTarget } from "https://dotland-vbqsvsrpfncg.deno-staging.dev/x/oak@v11.1.0/server_sent_event.ts?s=ServerSentEventTarget";

Properties

readonly
closed: boolean
[src]

Is set to true if events cannot be sent to the remote connection. Otherwise it is set to false.

Note: This flag is lazily set, and might not reflect a closed state until another event, comment or message is attempted to be processed.

Methods

close(): Promise<void>[src]

Close the target, refusing to accept any more events.

dispatchComment(comment: string): boolean[src]

Send a comment to the remote connection. Comments are not exposed to the client EventSource but are used for diagnostics and helping ensure a connection is kept alive.

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx) => {
   const sse = ctx.getSSETarget();
   sse.dispatchComment("this is a comment");
});

await app.listen();
dispatchMessage(data: unknown): boolean[src]

Dispatch a message to the client. This message will contain data: only and be available on the client EventSource on the onmessage or an event listener of type "message".

dispatchEvent(event: ServerSentEvent): boolean[src]

Dispatch a server sent event to the client. The event type will be sent as event: to the client which will be raised as a MessageEvent on the EventSource in the client.

Any local event handlers will be dispatched to first, and if the event is cancelled, it will not be sent to the client.

import { Application, ServerSentEvent } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx) => {
   const sse = ctx.getSSETarget();
   const evt = new ServerSentEvent("ping", "hello");
   sse.dispatchEvent(evt);
});

await app.listen();
dispatchEvent(event: CloseEvent | ErrorEvent): boolean[src]

Dispatch a server sent event to the client. The event type will be sent as event: to the client which will be raised as a MessageEvent on the EventSource in the client.

Any local event handlers will be dispatched to first, and if the event is cancelled, it will not be sent to the client.

import { Application, ServerSentEvent } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx) => {
   const sse = ctx.getSSETarget();
   const evt = new ServerSentEvent("ping", "hello");
   sse.dispatchEvent(evt);
});

await app.listen();