Emitter

mbostock edited this page Apr 22, 2012 · 2 revisions

WikiAPI ReferenceCollectorEmitter

Cube provides a JavaScript WebSockets client for posting events to a collector. This is handy for writing emitters: programs that send events to Cube. Here's a simple Node script for posting a single "random" event to a collector running on localhost:1080:

var cube = require("cube");

var client = cube.emitter("ws://localhost:1080");

client.send({
  type: "random",
  time: new Date(),
  data: {random: Math.random()}
});

client.close();

While you can easily write an emitter yourself using WebSockets, Cube's emitter is nice because it will automatically reconnect and retry on error. Also, if you ^C a daemon emitter and call client.close(), the emitter will wait until it has flushed all queued events before closing.

The emitter client can also be used to send events via UDP:

var cube = require("cube");

var client = cube.emitter("udp://localhost:1180");

client.send({
  type: "random",
  time: new Date(),
  data: {random: Math.random()}
});

client.close();

Note that with UDP there is little error detection: if the Collector isn't running, the emitter won't notice! This is nice if you want fire-and-forget events and the greatest possible throughput; on the other hand, if you want to notice when events are not received by the collector, then use the WebSocket interface instead.