Nuclear Powered Marshmallows
    Have ideas to improve npm?Join in the discussion! »

    cometd-cjs

    0.1.0 • Public • Published

    CometD library for CJS.

    This is little more than taking the CometdD JavaScript library and making each component available via module.exports.

    The org.cometd namespace is not needed. Neither is jQuery!

    However you need to set up the XHR yourself.

    This is based on the CometD 3.0.3 JavaScript library.

    Example

     
     
    const { CometD, LongPollingTransport, Transport, bindReloadExtension } = require('cometd-cjs');
     
    // You need to implement the XHR for the transports.
    function LongPoller() {
     
      const transport = Transport.derive(new LongPollingTransport());
     
      transport.xhrSend = packet => {
     
        const xhr  = new XMLHttpRequest();
        const data = JSON.parse(packet.body);
     
        xhr.open('post', packet.url);
     
        xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
     
        xhr.onreadystatechange = () => {
     
          if (xhr.readyState === 4) {
     
            if (xhr.status === 200) {
     
              packet.onSuccess(xhr.responseText);
     
            } else {
     
              packet.onError(xhr.statusText, xhr.response);
     
            }
     
          }
     
        };
     
        xhr.send(JSON.stringify(data));
     
      };
     
      return transport;
     
    }
     
    const cometd = new CometD();
     
    // The bindX functions return the "org" object with the given extension "bound".
    const { ReloadExtension } = bindReloadExension();
     
    cometd.registerTransport('long-polling', new LongPoller());
    cometd.registerExtension('reload', new ReloadExtension());
     
    cometd.init('<somewhere>/cometd');
     
    cometd.addListener('/meta/connect', message => {
     
      console.log(message);
     
    });
     

    References

    cometd at github

    cometd JavaScript library docs

    Also see

    cometd-jquery

    Install

    npm i cometd-cjs

    DownloadsWeekly Downloads

    0

    Version

    0.1.0

    License

    See https://github.com/cometd/cometd/tree/master/LICENSES

    Last publish

    Collaborators

    • avatar