public_radio

0.0.4 • Public • Published

Topology

Build highly distributed services by allowing your node applications to share emitted events over tcp socket connections.

How it works and what it's for

Applications can act as a server (allowing for incoming connections) or a client (connecting to a server). Public Radio allows you to create a graph of nodes made up of server and clients that can emit events that will be received by all the other nodes, as well as allowing them to bind callbacks to specific events they are interested in.

BuildStatus

Usage

Creating a server that will listen for incoming connections

var Server = require('public_radio').Server;
 
var server = new Server(5000);
 
server.listen();

Creating a client to connect to a server

var Client = require('public_radio').Client;
 
var client = new Client('localhost', 5000);
 
client.on('connected', function(conn) {
  // do something with connection
});
 
client.connect();

Connect servers to other servers to share your events with them and their clients

 
server.linkTo('localhost', 5001);
 

Setting up a listener for an event on a server

 
server.events.on('stock_update', function(symbol, price) {
  // work with stock update
});
 

Emitting an event from a server

 
server.broadcast('stock_update', 'GOOG', 15.43);
 

Setting up a listener for an event on a client

 
client.on('connected', function(conn) {
  conn.on('stock_update', function(symbol, price) {
    // work with stock update
  });
});
 

Emitting an event from the client

 
client.on('connected', function(conn) {
 
  conn.emit('stock_update', 'GOOG', 15.43);
 
});
 

or

 
client.connection.emit('stock_update', 'GOOG', 15.43);
 

Callbacks

The final argument of the emitted event can be a function

(replies have to be received within 60 seconds)

 
client.on('connected', function(conn) {
 
  conn.emit('stock_update', 'GOOG', 15.43, function(alert) {
    // do something with alert
  });
 
});
 

The listener can reply to the emitting node via a function passed as the final argument

 
server.events.on('stock_update', function(symbol, price, reply) {
  reply('SELL SELL SELL!');
});
 

So in this case the function passed to emit will be called with 'SELL SELL SELL!'

Bigger Picture

Topology

Setting up this network

 
var Server = require('public_radio').Server,
    Client = require('public_radio').Client;
 
var server1 = (new Server(5000)).listen(),
    server2 = (new Server(5001)).listen();
 
server1.linkTo('localhost', 5001);
 
var client1 = (new Client('localhost', 5000)),
    client2 = (new Client('localhost', 5000)),
    client3 = (new Client('localhost', 5001));
 
client1.on('connected', function(conn) {
 
});
 
client1.connect();
 
client2.on('connected', function(conn) {
 
});
 
client2.connect();
 
client3.on('connected', function(conn) {
 
});
 
client3.connect();
 

Credits

Big thanks to @zufferguffin for the delightfully awesome header image.

Readme

Keywords

none

Package Sidebar

Install

npm i public_radio

Weekly Downloads

0

Version

0.0.4

License

BSD

Last publish

Collaborators

  • bthesorceror