Examples

The following examples demonstrate Javascript peer API usage and (protocol) messages. They actually run in your browser. You can edit them on codepen if desired. The examples use a public Jet Daemon, which also hosts a Todo-App. Thus you may notice activity of other peers "playing" with it. To view everything in a nice debug-perspective, open Radar.

Todo-App Tutorial

For a basic understanding of how to write complete "client/server" apps, check out the Todo-App tutorial.

Connect

This example creates a peer and reports back the connection status. Daemons can run on the same machine as the Peer or on a remote site.

The most relevant code snippet is:

var peer = new jet.Peer({
      url: 'ws://jet.nodejitsu.com:80'
});

peer.connect().then(function() {

});

See the Pen Jet Connect by Gerhard Preuss (@lipp) on CodePen.

Add States

This example creates a Peer and adds two States with random. One, ignoring the Daemon response, the other one logging the Daemon response to console. The message traffic between the Daemon and the Peer is visible in the result window bottom.

The most relevant code snippet is:

// create read-only state (no set callback provided)
// ignore the Daemon response by leaving out the callback object.
// the request is a notification.
var foo = new jet.State('foo', 123);
peer.add(foo);

// create writable state,
// provide callback object with error and success handlers.
// the Daemon will send a response, as the request is not a
// notification.
var bar = new jet.State('bar', {age: 123});
bar.on('set', function(newVal) {
    if (newVal.age > this.value().age) {
      console.log('bar', newVal);
    } else {
      throw new Error('arg');
    }
});

See the Pen Jet Add State by Gerhard Preuss (@lipp) on CodePen.

Fetch Simple

This examples creates a Peer, adds two States with random name ("random...") and fetches everything with a path starting with "random". The message traffic between the Daemon and the Peer is visible in the result window bottom. Compare the (fetch) id of the fetch message with the incoming messages' method field. The incoming messages can be considered Passive as they are Requests targeted at the Peer, embedding a Peer defined method field value.

This is the most relevant snippet:

// fetch and provide: 1) fetch rule, 2) callback for fetching
var randomStuff = new jet.Fetcher()
  .path('startsWith', 'random')
  .on('data', function(path, event, value) {
  });

peer.fetch(randomStuff);

See the Pen Jet Fetch State by Gerhard Preuss (@lipp) on CodePen.

Change State

This examples creates a Peer, adds a "ticker" States with random name (so that at least these State are available for fetching) and fetches everything with a path starting with "ticker". Every second the ticker value is incremented by one. The message traffic between the Daemon and the Peer is visible in the result window bottom.

This is the most relevant snippet:

var ticker = new jet.State('ticker', 1);

peer.add(ticker).then(function() {
  setInterval(function() {
    var old = ticker.value();
    ticker.value(++old); // post state change
  }, 1000);
});

See the Pen Jet Change State by Gerhard Preuss (@lipp) on CodePen.