distributed pub/sub using hash rings
npm install vertex
make sure redis is installed and running
npm i vertex --save
you are good to go
`code example
`javascript
const { Server, Client } = require(vertex);var config = {
name: 'myTestServerName',//you get a silly name if you leave this out
logger: {
level: 'info'//info,trace,error
},
services: {
tcp: {
host: '127.0.0.1',//bind to local loopback
port: 60606//default is 60606, this is the internal tcp port that cluster comms for this node happens on
},
cluster: {
secret: 'myTestClusterSecret',//everyone must know the same secret to join
seed: true,//this is the first server we are starting
join: ['127.0.0.1:60606','127.0.0.1:60607','127.0.0.1:60608']//list of nodes to rediscover if one is dropped and restarts
},
http: {
port: 3737 //3737 by default - could be port 80, this is what the clients connect to
}
}
}
Server.create(config)
.then(server => {
var myServerInstance = server;
//able to accept client connections to now
var client = new Client({url: 'ws://localhost:3737'});
client.on('connection-confirmed', function () {
client.subscribe('/test/topic/*',
function(message){//this is the subscription handler
//message should be
//{
// "topic": "/test/topic/1",
// "data": "test"
//}
client.disconnect({code: 1000, reason: 'intentional disconnect test'});
})
.then(function(ref){
//you can unsubscribe using the ref
//client.unsubscribe(ref).then(...)
client.publish('/test/topic/1', {"test":"data"})
.then(function(results){
//you have published, data should appear in handler
})
})
});
client.connect();
})
.catch(err => {
});
`cli
`bash#STEP 0: get vertex installed, check for redis
make sure redis is installed and running
start 2 servers which form a cluster
git clone https://github.com/team-tenacious/vertex.git && cd vertex
npm i
#STEP 1: start our seed server
> node bin/vertex-server -e true
#NB: client and server cli will show a vertex> prompt
#NB: once in the vertex prompt, client and server help can be show by typing h and enter
#NB: once in the vertex prompt, client and server are stopped and exited by typing x and enter
#STEP 2: open ANOTHER command prompt in same vertex folder location
#start another server, clustered with the seed server
> node bin/vertex-server -c 60607 -p 3738 -m '127.0.0.1:60606'
#NB: for options run node bin/vertex-server -h
#STEP 3: open YET ANOTHER command prompt in same vertex folder location
> node bin/vertex-client
#NB: a client has been started and has joined the cluster via the first default server
#STEP 4: open YET STILL ANOTHER command prompt (you now have 4 windows open) in same vertex folder location
> node bin/vertex-client -e 127.0.0.1:3738
#NB: a client has been started and joined to the cluster via the second started server
#NB: again, for options run node bin/vertex-client -h
#STEP 5: now subscribe the current client to a topic
vertex>sub /test/*
#NB: output looks a bit like this:
subscribed to: /test/* ref: OrFHlcY1Rgaaf5qTbwhKQw/0
#STEP 6: switch to the previous client command prompt window (started by running "node bin/vertex-client")
#now publish some data to the topic you subscribed on the other client to
vertex>pub /test/1 test
#STEP 7: switch to other client session and check you received the message:
received publication: {
"topic": "/test/1",
"data": "test"
}
``NB: edge nodes can also be search nodes
NB: this does mean that / and are special characters and that /1/ is not the same as /1/*/2
Clients perform subscribe requests only once for subscription path and option combinations, following that a local refCount is incremented or decremented depending on subscribe or unsubscribes - when the local refCount reaches zero a cluster unsubscribe message happens. Edge nodes do something similar whereby the subscription record is only broadcasted once consecutive subscribes and unsubscribes are also managed through a refCount which only broadcasts an unsubscribe/subscribe when it is 0 or 1 respectively.