Code Monkey home page Code Monkey logo

event-sink's Introduction

EventSink

This module provides a complement to the EventSource interface defined as part of the Web APIs. The EventSink can be used in a server to provide a simple interface to sending Server-sent Events.

Usage

const sink = new EventSink();
const response = sink.getResponse(); // HTTP Response with appropriate headers for SSE

// Can pass in an event object
await sink.dispatchEvent({
  name: "test",
  content: "testing",
});

// Can pass in event name, content (and optional ID)
await sink.dispatchEvent("test2", "testing2", "2");

await sink.close;

History

An EventSink can take an EventHistory object. If this is provided, every dispatched event is pushed into the history object. Each entry is a single Server-sent event.

CircularBuffer

The CircularBuffer object implements the EventHistory (along with a number of other Array<T> methods).

It's designed to be a fixed size buffer that can be constantly added to and removed from. As such, it can retain the last N events.

This can be used to replay events for a client that has to reconnect and who sends the Last-Event-ID header.

Since it's a circular buffer, if it fills faster than it is drained, older events will get lost; however, this is generally reasonable behaviour for SSEs, since old events will have limited value.

event-sink's People

Contributors

dancrumb avatar hironichu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.