npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jenkins-cd/sse-gateway

v0.0.21

Published

Client API for the Jenkins SSE Gateway plugin. Browser UI push events from Jenkins.

Downloads

4,486

Readme

Server Sent Events (SSE) Gateway plugin for Jenkins.

Uses the pubsub-light-module jenkins-module to receive light-weight events and forward them into browser-land via SSE.

Install

npm install --save @jenkins-cd/sse-gateway

Requirements

This plugin requires Jenkins version 2.2 or later.

2.2+ is needed because it supports Servlet 3 asynchronous requests, which are needed for Server Sent Events.

Usage

The API is quite simple, allowing you to subscribe to (and unsubscribe from) Jenkins event notification "channels".

Subscribing to "job" channel events (basic)

The "job" channel is where you listen for events relating to Jenkins Jobs, all of which are enumerated in the Events.JobChannel Javadoc.

var sse = require('@jenkins-cd/sse-gateway');

// Connect to the SSE Gateway, providing an optional client Id.
var connection = sse.connect('myplugin');

// subscribe to all events on the "job" channel...
var jobSubs = connection.subscribe('job', function (event) {
    var event = event.jenkins_event;
    var jobName = event.job_name;

    if (event === 'job_run_ended') {
        var runStatus = event.job_run_status;
        var runUrl = event.jenkins_object_url;
        
        // Do whatever ....
    }    
});


// And some time later, unsubscribe using the return from the subscribe...
connection.unsubscribe(jobSubs);

Subscribing to "job" channel events (with a filter)

The above example subscribes to all events on the "job" channel i.e. all events for all jobs in the Jenkins instance. This may be what you want in some cases, but in many cases you are just interested in receiving specific events. To do this, you simply need to specify a "filter" when subscribing to the channel.

For example, to only receive "FAILURE" events for the "order-management-webapp-deploy" job:

var sse = require('@jenkins-cd/sse-gateway');

// Connect to the SSE Gateway, providing an optional client Id.
var connection = sse.connect('myplugin');

// Add a filter as the last parameter ...
var jobSubs = connection.subscribe('job', function (event) {
    // this event is only relating to 'order-management-webapp-deploy' ...
}, {
    job_name: 'order-management-webapp-deploy',
    job_run_status: 'FAILURE'
});

// And some time later, unsubscribe using the return from the subscribe...
connection.unsubscribe(jobSubs);

Handling connection errors

As is to be expected, the connection to Jenkins can be lost. To handle this situation, simply register an onError handler with the connection instance.

var sse = require('@jenkins-cd/sse-gateway');

// Connect to the SSE Gateway.
var connection = sse.connect('myplugin');

// Connection error handling...
connection.onError(function (e) {
    // Check the connection...
    connection.waitConnectionOk(function(status) {
        if (status.connectError) {
            // The last attempt to connect was a failure, so
            // notify the user in some way....
            
        } else if (status.connectErrorCount > 0) {
            // The last attempt to connect was not a failure,
            // but we had earlier failures, so undo
            // earlier error notifications etc ...
            
            // And perhaps reload the current page, forcing
            // a login if needed....
            setTimeout(function() {
                window.location.reload(true);
            }, 2000);
        }
    });
});

// etc...

Note that only one handler can be registered per connection instance.

Note how the supplied connection.onError handler makes a call to connection.waitConnectionOk. connection.waitConnectionOk takes a connection status callback handler. This handler is called periodically until the connection is ok again i.e. it can be called more than once, constantly getting feedback on the connection state.

Internet Explorer Support

As always with Internet Explorer, there are issues. It doesn't support the SSE EventSource so in order to use it on Internet Explorer, please make sure that a polyfill is added to the page before your app. We have used this one and found it to work fine.

To add this polyfill to your .jelly file, simply include the following adjunct as early as possible.

<st:adjunct includes="org.jenkinsci.plugins.ssegateway.sse.EventSource" />

SSE Events in headless JavaScript environments

To use this API in a headless/non-browser JavaScript environment (e.g. server-side JavaScript, or a test environment), just require the headless-client e.g.:

var sse = require('@jenkins-cd/sse-gateway/headless-client');

// etc....

Browser Diagnostics

The SSE Gateway client code uses the @jenkins-cd/logging package for client-side/browser logging. See the Browser Configuration docs for how to configure logging in your browser, configuring the stored value of jenkins-instance/logging/categories:org.jenkinsci.sse for SSE logs.

Sample Plugin

See the sse-gateway-sample-plugin.