• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Build your own small (or larger) peer to peer network with node.js

smokesignal

Scale your real-time app with p2p message passing.

  • Simple: Broadcast stuff or just speak to a specific peer.
  • Autonomic: Have your nodes gather and reorder automatically.
  • Flexible: You define, how your nodes will behave.
  • Self-healing: Auto-detection of netsplits and disconnect events.

Smokesignal is not a gossip protocol. It does not implement p2p data replication for you. It is a plain, flexible peer-to-peer networking solution, onto which you can easily build your own replication model or use some event passing mechanism.

To faciliate this, direct connections from peer to peer as well as a network-wide broadcast (Node.broadcast) are simple duplex streams. Thus you can add all favours of stream-goodness, like remote-events, p2p-rpc-stream, and what not.

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 notified when your node added or removed a peer. 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

Synopsis

var smoke = require('smokesignal')


var node = smoke.createNode({
  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...

node.on('connect', function() {
  // Hey, now we have at least one peer!
  
  // ...and broadcast stuff -- this is an ordinary duplex stream!
  node.broadcast.write('HEYO! I\'m here')
})

node.on('disconnect', function() {
  // Bah, all peers gone.
})

// Broadcast is a stream
process.stdin.pipe(node.broadcast).pipe(process.stdout)

// Start the darn thing
node.start()

// mah, i'd rather stop it
node.stop()

Check out the complete chat app example!

API

smoke.createNode(opts:object)

Creates a new node. Options:

  • address: (compulsory) Your current ip address
  • port: (compulsory) Port to bind at
  • minPeerNo: (optional; default: 3) how many peers this node will actively try to bond with -- you can always connect to more manually!
  • maxPeerNo: (optional; default: 5) how many peers this node will accept at max. Infinity for no limit
  • seeds: (optional) an array of known nodes, that are part of the network, e.g. {port: 0, address: '127.0.0.1'}
  • pingTimeout: (optional; default: 3000) The time span in ms after which we consider the ping as failed
  • logger: (optional; default: empty object) An object that may provide the following methods: trace, debug, info, warn, error, fatal

Class: Node

Event: connect

Emitted when we have at least one peer.

Event: disconnect

Emitted when the last peer disconnects.

Node#broadcast

A duplex stream. Everyone will get what you write to it, and you'll get everything other people write to it also here.

Node#start()

Starts the node. The tcp server will be bound to the specified port and the node will try to enter the network.

Node#stop()

Stops the node. Will disconnect all peers and shut down the tcp server.

Node#addPeer(address:string, port:int)

Tries to connect to the node at the specified address and add it as a peer. This should allow people to pass a callback..

Node#peers

An instance of Peerlist

Class: Peerlist

Event: add

Emitted when a peer is added. This event is triggered with the corresponding peer object as the first parameter.

Event: remove

Emitted when a peer is removed. This event is triggered with the corresponding peer object as the first parameter.

Peerlist#inList(my_peer:Peer)

Returns a boolean indicating whether you're currently friends with that peer.

Peerlist#list

An array containing all nodes you're friends with. Please don't manipulate this directly. Instead, use Node#addPeer to try and connect to a node and Peer#close

Class: Peer

A duplex stream. Write something to it and the other end will get it out of their representational Peer object for your node. Vice versa, if the other side writes something to their object, you'll be able read it here.

Event: end

Emitted when all ties to this node have been cut.

Peer#remoteAddress

The remote address of this peer.

Peer#remotePort

The remote port of this peer.

Peer#id

The network-wide id of this peer.

Peer#close

Cut's everything that ties you to this node.

Todo

  • Use event-loop-friendly nextTick call(back)s
  • Maybe make options.port optional, so it just uses an available port

Legal

(c) 2012-2013 Marcel Klehr MIT License

Changelog

0.2.1

  • Correctly inherit from EventEmitter

0.2.0

  • Replace socket.io-like interfaces with proper duplex streams

0.1.0

  • Don't depend on log4js

More Repositories

1

toposort

Topologically sort directed acyclic graphs (such as dependency lists) in javascript
JavaScript
296
star
2

changesets

Changeset library with operational transformation -- for node and the browser!
JavaScript
281
star
3

vdom-virtualize

Virtualize a DOM node
JavaScript
131
star
4

tivoka

JSON-RPC client/server library for PHP (supports v1.0 and v2.0 specs)
PHP
74
star
5

dom-ot

Transform DOM tree patches against each other (operational transformation)
JavaScript
51
star
6

buzzmap

draw and edit mindmaps interactively, using force-directed layouts (jQuery plugin)
JavaScript
22
star
7

prism.io

Share and edit documents in real-time. Superseded by http://github.com/marcelklehr/gulf
JavaScript
18
star
8

chordreader

Search for, display, transpose and save chords on your phone, that you get from the interwebs. 🎢
Java
13
star
9

beardless

Beardless templating for node.js. Start shaving!
JavaScript
12
star
10

warp

Proof of concept dom-ot editor -- superseded by https://github.com/gulf and https://github.com/hivejs
JavaScript
9
star
11

r-string

Commutative conflic-free replicated String (CRDT/Scuttlebutt)
JavaScript
7
star
12

magnet

An experiment: A duck-typed, object-oriented, syntax-driven (and potentially awesome) programming language.
JavaScript
6
star
13

zoocache

Output cache that neatly integrates into your PHP application.
PHP
6
star
14

p2p-rpc-stream

rpc streams for node. done right (TM)
JavaScript
5
star
15

vdom-serialize

JavaScript
5
star
16

y-connector-dat

A dat transport for y.js
JavaScript
4
star
17

PeerPad

peer-to-peer collaborative editing
JavaScript
3
star
18

ep_push2delete

delete your etherpad with one click.
JavaScript
3
star
19

minetest-conveyor

Conveyor mod for minetest, the awesome voxel mining game.
Lua
3
star
20

ep_infopanel

An "about" section for Etherpad-lite. Donuts do usually help.
JavaScript
3
star
21

waterline-to-jsonapi

Convert your waterline query results to jsonapi compliant responses.
JavaScript
3
star
22

intervarl

A variable interval. The timeout period can be changed dynamically.
JavaScript
2
star
23

ot-socialcalc

Operational transformation for socialcalc commands (shareJS compatible)
JavaScript
2
star
24

observ-emitter

an observable atomic emitter
JavaScript
2
star
25

browser-stream

Node.js streams in your browser
JavaScript
2
star
26

mutation-summary

Makes observing the DOM fast and easy
JavaScript
2
star
27

deep-reinforcement-learning-2022

Course work for the Deep Reinforcement Learning Course in Spring 2022 at University OsnabrΓΌck
Python
2
star
28

umbilical

Bidirectional rpc over tcp for node.js
JavaScript
1
star
29

magpie-typicality-mt

HTML
1
star
30

domnode-at-path

Supply a path and a root node and receive the domnode at that path.
JavaScript
1
star
31

ep_timeslider

The timeslider of etherpad lite, extracted and squashed into a plugin...
JavaScript
1
star
32

lseq-between

Sort sequences without re-indexing on insert ✨
JavaScript
1
star
33

ep_ether-o-meter

Display metrics. For etherpad. In real-time. With swag.
HTML
1
star
34

obj-to-argv

Takes an object and spits out the argv/args array as libraries like optimist would expect it.
JavaScript
1
star
35

theatre

my own, chocolate-flavoured lisp dialect
JavaScript
1
star
36

path-to-domnode

JavaScript
1
star
37

nextcloud-apps-ranking

Nextcloud apps listed by popularity
JavaScript
1
star
38

wutsdis

πŸ§™ Auto-tag your photo collection using an ImageNet object detection model.
Python
1
star
39

shoe2

Binary-safe sockJS streams with all necessary events
JavaScript
1
star
40

atomic-emitter

Emitters as values that you can pass around and assign, optionally with private event writing access.
JavaScript
1
star