Giter VIP home page Giter VIP logo

jquery-sse's Introduction

jQuery SSE

Build Status Opensource ByJG GitHub source GitHub license GitHub release

A lightweigth jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill. This plugin try to use the native EventSource object if it supported by the browser. If there is no native support the request is made by ajax requests (polling). You do not need to change the server side nor the client side.

If you are looking for a SSE Polyfill library without jQuery dependency try yaj-sse. The yaj-sse is a port from version 0.1.4 of jQuery SSE.

Example

Client Side

var sse = $.SSE('http://example.com/sse-server.php', {
    onMessage: function(e){ 
        console.log("Message"); console.log(e); 
    }
});
sse.start();

Server Side

echo "data: My Message\n";
echo "\n";

Dependencies

  • jQuery

Install

Just download the repository and point to the jQuery plugin:

<script src="jquery.sse.js" ></script>

or

<script src="jquery.sse.min.js" ></script>

You can also install using bower:

bower install jquery-sse

Usage:

Constructor

var sse = $.SSE(url, settings);
  • url: URL for the server will be sent the events to this page;
  • settings: The events and options for the SSE instance

Settings List

All the options:

var sseObject = $.SSE('sse-server.php', {
    onOpen: function (e) {},
    onEnd: function (e) {},
    onError: function (e) {},
    onMessage: function (e) {},
    options: {},
    headers: {},
    events: {}
});

Event onOpen

Fired when the connection is opened the first time;

onOpen: function(e){ 
    console.log("Open"); console.log(e); 
},

Event onEnd

Fired when the connection is closed and the client will not listen for the server events;

onEnd: function(e){ 
    console.log("End"); console.log(e); 
},

Event onError

Fired when the connection error occurs;

onError: function(e){ 
    console.log("Could not connect"); 
},

Event onMessage

Fired when the a message without event is received

onMessage: function(e){ 
    console.log("Message"); console.log(e); 
},

Custom Options

Define the options for the SSE instance

options: {
    forceAjax: false
},
  • forceAjax: Uses ajax even if the EventSource object is supported natively;

Custom Events

Fired when the server set the event and match with the key

For example, if you have a custom event called myEvent you may use the follow code:

events: {
    myEvent: function(e) {
        console.log('Custom Event');
        console.log(e);
    }
}

Server side:

echo "event: myEvent\n";   // Must match with events in the HTML.
echo "data: My Message\n";
echo "\n";

Custom Headers

You can send custom headers to the request.

headers: {
    'Authorization': 'Bearer 1a234fd4983d'
}

Note: As the EventSource does not support send custom headers to the request, the object will fallback automatically to 'forceAjax=true', even this it is not set.

Methods

start

Start the EventSource communication

sse.start();

stop

Stop the EventSource communication

sse.stop();

Quirks

The ajax does not support the streaming as the event source supports. In that case we recommend create a server without streaming and set the "retry" to determine query frequency;

Example Server Side:

echo "retry: 3000\n";
echo "data: My Message\n";
echo "\n";

Minify

apt install uglifyjs

uglifyjs --compress 'drop_console,drop_debugger' --mangle -r '$,require,exports,_' -o jquery.sse.min.js jquery.sse.js

Running the examples

Start the webserver:

docker run -it --rm -p 8080:80 -v $PWD:/var/www/html byjg/php:7.4-fpm-nginx 

Open the browser: http://localhost:8080/examples/sse-client.html

References


Open source ByJG

jquery-sse's People

Contributors

byjg avatar thomasdotcodes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jquery-sse's Issues

Only last event will be triggered in Edge/IE

When you send a contiguous stream of events (instead of calling a script over and over again), only the last one will be triggered. This is not in sync with Firefox/Chrome and not as I expect it.

npm package 0.1.4 wrong version?

Hi!
I was having an issue similar to #8 , with the npm package version 0.1.4, but on comparing with master here it seems that it's the 0.1.3 version, could it be there was a mistake in deploy?

Incorrect variable name in onerror() in createEventSource()

Typo? Should event be e here?

https://github.com/byjg/jquery-sse/blob/master/jquery.sse.js#L89

    // Private Method for Handle EventSource object
    function createEventSource(me) {
        me.type = 'event';
        me.instance = new EventSource(me._url);
        me.instance.successCount = 0;

        me.instance.onmessage = me._settings.onMessage;
        me.instance.onopen = function (e) {
            if (me.instance.successCount++ === 0) {
                me._settings.onOpen(e);
            }
        };
        me.instance.onerror = function (e) {
            if (event.target.readyState === EventSource.CLOSED) {   // <--- event should be e?
                me._settings.onError(event);                                        // <--- event should be e?
            }
        };

Unable to set property data in Edge/IE

SCRIPT5007: Unable to set property 'data' of undefined or null reference in jquery.sse.js (156,32)

MCVE:

var sse = $.SSE("test.php", {
    onMessage: function() {
        sse.stop();
    }
});
sse.start();

Update: I think this is related to the fact, that I do send multiple events per script call, not just one.

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.