Giter Site home page Giter Site logo

eventsimulator's Introduction

eventSimulator

Browser based SSE Event caching and replay support

Include the js eventSimulator source code

<script src="eventSimulator.js"></script>

Create a new EventSimulator object with configuration

SIM = new EventSimulator({
  /*
   * The eventSource URL to publish event to
   */
  event_source_url: "event-simulator-sse",

  /*
   * The SSE URL to capture events from
   */
  capture_url: "UrlToSSE_Stream",

  /* 
   * The localstore to use as the destination for saving captured events,
   * and the source for the events to publish to event_source_url
   */
  localstore_name: "capturedEvents",

  /* If True, send a special message {id : 'CLOSE'} after all stored events
   * have been sent out and before the Stream is closed.
   *
   * If False, close the stream after all stored events have been consumed.
   */
  close_event_supported: true,
});

register() for starting the service worker

/*
  * Calling `register` will register a service-worker and publish 
  * an `EventSource()`` compatible URL.
  *
  * The URL can be specified using the parameter `event_source_url`.
  * 
  */
SIM.register()
.then(function () {
  console.log("Registration Completed...");
})
.catch((error) => {
  console.error("Registration failed", error);
});

captureEvents() for capturing events from an external SSE source

/*
 * Calling `captureEvents` will open an EventSource(`capture_url`)
 * event.data from all incoming events will be saved to 
 * localStorage(`localstore_name`)
 */
SIM.captureEvents();

loadEventsFromStorage() for loading cached events from localStore

/*
* Calling `loadEventsFromStorage` will load the cached events from
* localStorage(`localstore_name`) and publish them to the service-worker.
* 
* The Service worker will then start publishing each event to `event_source_url`.
* The event messages will be dispatched with a delay of 2 seconds between messages.
* 
*/
SIM.loadEventsFromStorage();

test(EventSource URL) for testing the sanity of the replay

/*
* Calling `test(EventSource URL)` will connect to the specified event source URL.
* It will print the contents of each message that it receives.
*
* In case it receives a special message {id : 'CLOSE'}, it'll close the source stream.
* 
*/
SIM.test("event-simulator-sse")
.then(function () {
  console.log("Test Returned success");
})
.catch((error) => {
  console.error("Test Returned failure:", error);
});

eventsimulator's People

Contributors

79man avatar

Stargazers

 avatar

Watchers

 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.