Skip to content

pekkis/cometd-cjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

let { CometD, LongPollingTransport, Transport } = require('cometd');

// You need to implement the XHR for the transports.
function LongPoller() {

  let transport = Transport.derive(new LongPollingTransport());

  transport.xhrSend = function(packet) => {

    let xhr  = new XMLHttpRequest();
    let 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;

}

let cometd = new CometD();

cometd.registerTransport('long-polling', new LongPoller());

cometd.init('<somewhere>/cometd');

cometd.addListener('/meta/connect', (message) => {

  console.log(message);

});

Todo

  • Look into using EventEmitters in place of the above boilerplate.
  • Ensure all the extenstions work as expected.
  • Provide more transport examples.

References

cometd at github

cometd JavaScript library docs

Also see

cometd-jquery

About

The cometd JavaScript library for cjs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%