Tag: NodeJS

  • CometD and NodeJS, part 2

    In ourĀ previous blog, we presented the case of a Webtide customer, Genesys, that needed to integrate CometD in NodeJS and how we developed a CometD client capable of running in the NodeJS environment.
    In this article we present the other side of the solution, that is, how we implemented the CometD NodeJS Server. Leveraging this, Genesys was able to use the standard CometD JavaScript Client in the browser front-end application to talk to the CometD NodeJS server application, which in turn used the CometD NodeJS Client to talk to the Java CometD server application.
    The CometD NodeJS Server is based on the same CometD concepts present in the CometD Java server.
    In particular, there is a central object, the CometDServer, that handles HTTP requests and responses provided by the NodeJS environment. The CometDServer object is also a repository for sessions and channels, that are the two primary concepts used in a server-side CometD application. Both sessions and channels emit events that an application can listen to in order to implement the required business logic.
    Installing the CometD NodeJS Server is easy:

    npm install cometd-nodejs-server
    

    The minimal setup of a CometD NodeJS Server application is the following:

    var http = require('http');
    var cometd = require('cometd-nodejs-server');
    var cometdServer = cometd.createCometDServer();
    var httpServer = http.createServer(cometdServer.handle);
    httpServer.listen(0, 'localhost', function() {
        // Business logic here.
    });
    

    Now you can use the CometD NodeJS Server APIs to be notified when a message arrives on a certain channel:

    var channel = cometdServer.createServerChannel('/service/chat');
    channel.addListener('message', function(session, channel, message, callback) {
        // Your message handling here.
        // Invoke the callback to signal that handling is complete.
        callback();
    });
    

    Further examples of API usages can be found at the CometD NodeJS Server project.
    With the CometD NodeJS Client and Server projects, Genesys was able to leverage CometD throughout the whole process chain, from the browser to NodeJS to the Java CometD server. This allowed Genesys the use of a consistent API throughout the whole architecture, with the same concepts and a very smooth learning curve for developers.

  • CometD and NodeJS, part 1

    In addition to our Lifecycle Support offerings, Webtide is also committed to helping develop new functionality to meet customer needs for the open source projects Webtide supports, CometD and Eclipse Jetty.
    Recently Genesys, a global leader in customer experience solutions and one of Webtide’s customers, reached out regarding their usage of CometD, looking for help integrating CometD with NodeJS.
    Their architecture had a browser front-end application talking to a NodeJS server application, which in turn talked to a Java CometD server application. Server-side events emitted by the CometD application needed to travel through NodeJS all the way down to the front-end, and the front-end needed a way to register interest for those events.
    At the time the CometD project did not have any NodeJS integration, so Genesys partnered with Webtide to develop the integration as a sponsored effort, leveraging our knowledge as the experts behind CometD.
    This resulted in two new CometD sub-projects, CometD NodeJS Client and CometD NodeJS Server, and in publishing CometD artifacts in NPM.
    The first step was to publish the CometD JavaScript Client to NPM. Starting with CometD 3.1.0, you can now do:

    npm install cometd

    and have the CometD JavaScript Client available for developing your front-end applications.
    However, the CometD JavaScript Client does not run in NodeJS because it assumes a browser environment. In particular it assumes the existence of the window global object, and of the XMLHttpRequest APIs and functionalities such as HTTP cookie handling.
    Initially, rewriting a pure NodeJS CometD client was considered, but discarded as it would have duplicated a lot of code written with years of field experience. It turned out that implementing the parts of the browser environment needed by the CometD JavaScript Client was simpler, and the CometD NodeJS Client was born.
    The CometD NodeJS Client implements the minimum requirements to run the CometD JavaScript Client inside a NodeJS environment. It uses the NodeJS HTTP facilities to implement XMLHttpRequest, exposes a window global object and few other functionalities present in a browser environment such as timers (window.setTimeout(...)) and logging (window.console).
    Writing a CometD NodeJS client application is now very simple. First, install the CometD client libraries:

    npm install cometd-nodejs-client
    npm install cometd
    

    Second, write your application:

    require('cometd-nodejs-client').adapt();
    var lib = require('cometd');
    var cometd = new lib.CometD();
    ...
    

    Following this framework, Genesys was able to utilize CometD from within NodeJS to talk to the Java CometD server application and vice-versa.
    In the next blog we will take a look at the CometD NodeJS Server which allows the front-end application to talk to the NodeJS server application, therefore using CometD from the front-end application through NodeJS to the Java CometD server.