simplebroadcast
Version:
Simple broadcasting and repeatign of JSON messages using net sockets
90 lines (64 loc) • 2.54 kB
Markdown
Simple broadcasting of JSON messages using net sockets.
Via npm on Node:
```
npm install simplebroadcast
```
Reference it from your program:
```js
var simplebroadcast = require('simplebroadcast');
```
Broadcaster (server) side
```js
var broadcaster = simplebroadcast.createBroadcaster();
broadcaster.listen(8000);
```
Client side
```js
var client = simplebroadcast.createClient();
client.on('message', function(message) {
// message processing
});
client.connect(port, host);
// broadcasting a message, after connection
client.on('connect', function() {
client.write(msg);
}
```
A broadcaster can connect to another broadcaster. Each one is the cliente of the other.
```js
broadcaster.connect(port, host));
```
You can build a tree of broacasters. A tree is a graph without cycles. If the graph of broadcasters has a cycle, you messages will be send forever.
A broadcaster can act as a repeater, listening to other repeateres, or connecting as a repeater to another one.
```js
broadcaster.listenRepeater(8001);
broadcaster.connectRepeater(8002, 'repeater2');
```
The messages sent by a repeater are sent to all clients, but not to other repeaters. The messages sent by a client are sent to the others clients and to all
repeaters. In this way, you can build a graph of repeaters, including cycles. Usually it is an star graph were each repeater is connected to all the others.
```
git clone git://github.com/ajlopez/SimpleBroadcast.git
cd SimpleBroadcast
npm install
npm test
```
[](https://github.com/ajlopez/SimpleBroadcast/tree/master/samples/Broadcast) sample shows
how you send and receive messages to/from many clients thru a broadcast server.
- 0.0.1: Published
- 0.0.2: Published
- 0.0.3: Published, updated to use SimpleMessages 0.4. Adding error event to client in code.
- 0.0.4: Published, updated to use SimpleMessages 0.6
Feel free to [file issues](https://github.com/ajlopez/SimpleBroadcast) and submit
[](https://github.com/ajlopez/SimpleBroadcast/pulls) � contributions are
welcome.
If you submit a pull request, please be sure to add or update corresponding
test cases, and ensure that `npm test` continues to pass.
(Thanks to [JSON5](https://github.com/aseemk/json5) by [aseemk](https://github.com/aseemk).
This file is based on that project README.md).