Permalink
Browse files

Update README and example chat app

  • Loading branch information...
1 parent 7ec7bae commit 3a1b885d609d9944a7acbbfdeb4d7445a851afe6 @marcelklehr committed Mar 11, 2013
Showing with 39 additions and 20 deletions.
  1. +29 −13 README.md
  2. +2 −2 example/ChatApp/flooding-peer_benchmark.js
  3. +8 −5 example/ChatApp/peer.js
View
42 README.md
@@ -4,10 +4,15 @@ Scale your real-time app with p2p message passing.
* *Simple*: Broadcast events or just speak to a specific peer.
* *Autonomic*: Have your nodes gather and reorder automatically.
-* *Configurable*: If you provide your node with a network entry point, it'll join some peers automatically. Alternatively you can simply connect manually.
+* *Flexible*: You define, how your nodes will behave.
* *Self-healing*: Auto-detection of netsplits and disconnect events.
-This is currently in development. Most of the things below work already, though...
+Smokesignal is not a gossip protocol. It does not implement p2p data replication for you. It is a plain, flexible peer-to-peer event passing solution, onto which you can easily build your own replication model.
+
+You can connect to new peers manually or allow your node to search for peers automatically. You can listen on peer list events to get notfied when your node added or removed a peer. You can emit events directly to a specific peer or broadcast them to the whole network. If you want, your node will automatically ping the network seed(s) once in a while, to detect netsplits and resolve them automatically.
+Nodes emit a `connect` event when the node adds the first peer, and a `disconnect` event, when the last peer disconnects. If you have specified some seeds, your node will automatically rejoin the network in this case.
+
+This project is in development, so be prepared that things might break in some situations.
```
npm install smokesignal
@@ -20,27 +25,38 @@ var smoke = require('smokesignal')
var node = smoke.createNode({
- port: parseInt(process.argv[2]) || 13
-, entrance: {port: 13, address:'192.168.2.100'} // the address of a known node
-, localNetmask: '192.168.2.1/255.255.255.0' // Supply it with your subnet mask and it'll figure out your IP
+ port: 8495
+, address: smoke.localIp('192.168.2.1/255.255.255.0') // Tell it your subnet and it'll figure out the right IP for you
+, seeds: [{port: 13, address:'192.168.2.100'}] // the address of a seed (a known node)
})
-// listen on network events
+// listen on network events...
node.on('connect', function() {
- console.log('Connected.')
+ // Hey, now have at least one peer!
- // and broadcast stuff
- node.broadcast.emit('my network', 'HEYO! I\'m here')
+ // ...and broadcast stuff
+ node.broadcast.emit('chat', 'HEYO! I\'m here')
})
node.on('disconnect', function() {
- console.log('mh. Disonnected.')
+ // Bah, all peers gone.
})
-node.broadcast.on('my network', function(msg) {
- console.log(msg)
+node.broadcast.on('chat', function(msg) {
+ // oho, a chat message!
})
+
+// Start the darn thing
+node.start()
+
+// mah, i'd rather stop it
+node.stop()
```
-Check out the complete [chat app example](https://github.com/marcelklehr/smokesignal/tree/develop/example/ChatApp)!
+Check out the complete [chat app example](https://github.com/marcelklehr/smokesignal/tree/develop/example/ChatApp)!
+
+## Todo
+
+ * Use event-loop-friendly nextTick call(back)s
+ * Remove log4js dep
View
4 example/ChatApp/flooding-peer_benchmark.js
@@ -2,8 +2,8 @@ var smoke = require('../../index')
var node = smoke.createNode({
port: parseInt(process.argv[2]) || 14
-, entrance: {port: 14, address:'192.168.2.100'}
-, localNetmask: '192.168.2.1/255.255.255.0'
+, address: smoke.localIp('192.168.2.1/255.255.255.0')
+, seeds: [{port: 14, address:'192.168.2.100'}]
})
View
13 example/ChatApp/peer.js
@@ -1,14 +1,17 @@
var smoke = require('../../index')
+ , log4js = require('log4js')
+
+log4js.setGlobalLogLevel('info')
process.stdin.setEncoding('utf8')
var node = smoke.createNode({
port: parseInt(process.argv[2]) || 14
-, entrance: {port: 14, address:'192.168.2.100'}
-, localNetmask: '192.168.2.1/255.255.255.0'
+, address: smoke.localIp('192.168.2.1/255.255.255.0')
+, seeds: [{port: 14, address:'192.168.2.100'}]
})
-console.log('Port', parseInt(process.argv[2]) || 14)
-console.log('IP', node.remoteAddress)
+console.log('Port', node.options.port)
+console.log('IP', node.options.address)
console.log('ID', node.id)
var prompt = function() {
@@ -30,7 +33,7 @@ node.on('disconnect', function() {
// Send message
process.stdin.on('data', function(d) {
- node.broadcast.emit('chat', node.remoteAddress+': '+d);
+ node.broadcast.emit('chat', node.options.address+': '+d);
prompt()
})

0 comments on commit 3a1b885

Please sign in to comment.