Giter Club home page Giter Club logo

instantly's Introduction

Instantly

instantly

Code Climate Test Coverage Travis Known Vulnerabilities

What is this?

EventSource wrapper.

The EventSource API is easy as it is. Reason I created this was to hide all logic used when I implement EventSource.

IMPORTANT. This is NOT EventSource Polyfill!

Installation

It's available on npm.

npm install --save instantly

How can I use this library?

It's an UMD module. If you don't know what UMD is: https://github.com/umdjs/umd

The UMD build is also available on unpkg:

<script src="//unpkg.com/instantly/dist/instantly.umd.js"></script>

Usage

// Example
var es = new Instantly('http://your-sse-endpoint.codes/channel', {
    origin: 'http://your-sse-endpoint.codes', // Optional. Just an extra level of precaution to verify your event origin matches your app's origin.
    retries: 2, // Optional. Default: 5 retries if connection to your endpoint fails.
    timeout: 1000, // Optional. Default: 15 seconds (15000). This is how often we should retry.
    closeConnNotFocus: true, // Optional. Default: false. This will close the SSE connection if the tab/window is not in focus. Will reconnect when in focus.
    error: function(err) {
        console.log(err);
    }, // Optional. Extending the internal error handler.
    open: function(event) {
        console.log(event);
    }, // Optional. Extend when you open a connection to SSE.
    close: function() {
        console.log('closed');
    }, // Optional. Extend when a connection to SSE is closed. (Usually when an error occur)
    injectEventSourceNode: require('eventsource'), // Optional. If the module is being used in Node you're able to inject [eventsource-node](https://www.npmjs.com/package/eventsource)
});

// If you want to use default options
// var es = new Instantly('http://your-sse-endpoint.codes/channel');

// Listen to messages without any event set
es.on('message', function newMessage(msg) {
    console.log(msg.data);
});

// Listen to messages with an event set
es.on('eventName', function newMessage(msg) {
    console.log(msg.data);
});

// Start to listen for events send by SSE
es.listen();

If you need to close the connection client side

// Close
es.close();

// Need to open the connection again?
es.listen();

Built in features

  • If you send an event with event id 'CLOSE', it'll close your SSE connection with no retries.

Example

Navigate to example folder

npm install
npm start

Open your browser in http://localhost:1337

High performance SSE server

SSEHub (Server-Sent Events streaming server)

Contribution

Contributions are appreciated.

License

MIT-licensed. See LICENSE.

instantly's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kferran

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.