https://github.com/unitech/synapsis
https://github.com/unitech/synapsis
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/unitech/synapsis
- Owner: Unitech
- Created: 2018-01-20T12:39:31.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-26T15:00:55.000Z (almost 8 years ago)
- Last Synced: 2025-06-09T11:45:39.408Z (7 months ago)
- Language: JavaScript
- Size: 22.5 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Synapsis
Inter connect local machines together with a Express like socket router.
Data transfer is cipphered with Diffie Hellman algorithm (dynamic public / private key exchange between peers based on a shared password).

## Usage
### Instanciate Peer
Instanciate new peer:
```javascript
var Synapsis = require('synapsis');
var synapse = new Synapsis({
namespace: 'namespace-name',
password: 'long-password-16-random-caracters-recommended',
// Identity will be shared to all peers
identity: {
name : require('os').hostname()
}
});
// Now start the peer
synapse.start();
```
### Expose Socket Routes
Now expose socket routes that can be triggered by other peers:
```javascript
synapse.router('command:restart', () => {
console.log('Got Restart');
});
synapse.router('command:sync_db', (new_db, reply) => {
fs.writeFile('current_db.json', new_db, (err) => {
if (err)
return reply(err);
reply(null, 'DB Synced');
});
});
```
### Send/Broadcast Actions to Peers
To send actions to other peers:
```javascript
// Broadcast a message to all nodes (w/o current) and do not wait for response
synapse.broadcast('command:restart');
// Broadcast a message + data to all nodes (w/o current) and do not wait for response
synapse.broadcast('command:restart', { some : 'data' });
// Broadcast a message to all nodes (w/o current) and receive messages from each (RPC like)
synapse.broadcast('command:sync_db', { my : { db : true } }, function(err, response, socket) {
console.log(`Err= ${err} Response = ${response} from = ${socket.identity.name}`);
});
```
Or send to a specific peer:
```javascript
var peer = synapse.getPeers()[0];
synapse.send(peer.id, 'command:restart');
synapse.send(peer.id, 'command:restart', function(err, data, socket) {
console.log(err, data, socket.identity);
});
synapse.send(peer.id, 'command:restart', {mu : { db : true} }, function(err, data, socket) {
console.log(err, data, socket.identity);
});
```
### Event Handling
```javascript
synapse.on('peer:connected', function(identity, socket) {
console.log('New peer detected', identity);
});
synapse.on('peer:disconnected', function(identity) {
console.error(identity);
});
synapse.on('error', function(err) {
console.error(err);
});
synapse.on('ready', function() {
console.log('Peer Ready');
});
```
## Test
Checkout test/ folder for more examples/
## License
MIT