Skip to content

Instantly share code, notes, and snippets.

@valerysntx
Created September 6, 2017 17:59
Show Gist options
  • Save valerysntx/96e7130bb978a3fa611dd0349967c97d to your computer and use it in GitHub Desktop.
Save valerysntx/96e7130bb978a3fa611dd0349967c97d to your computer and use it in GitHub Desktop.
mubpub publish / subscribe using mongodb channels
var mubsub = require('mubsub');
const uuid = require('uuid');
let url = 'mongodb://bellawe.com:27017/renderProtocol';
var clientA = mubsub(url);
var clientB = mubsub(url);
var inbound = clientA.channel('inbound', {recreate: true });
var outbound = clientB.channel('outbound', {recreate: true });
clientA.on('error', console.error);
inbound.on('error', console.error);
clientB.on('error', console.error);
outbound.on('error', console.error);
function send(channel, data){
channel.publish('protocol', data);
}
inbound.subscribe('protocol', function (message) {
let msg = message;
console.log(`${Date.now().toString()} inbound received: ${JSON.stringify(msg)}`);
switch(msg.action){
case 'sendFile':
{
send(outbound,{
action: 'fileReceived',
message: {
status: 'ok'
}
});
break;
};
case 'renderJobStarted':
{
send(outbound, {
action: 'waitingJobResults',
message: {
uuid: msg.uuid
}
});
break;
};
}
});
outbound.subscribe('protocol', function (message) {
let msg = message;
console.log(`outbound received: ${Date.now().toString()} ${JSON.stringify(msg)}`);
switch(msg.action){
case 'sendFile':
{
inbound.publish('protocol',
{
action: 'fileReceived',
message: {
status: 'ok'
}
});
break;
};
case 'fileReceived':
{
inbound.publish('protocol',
{
action: 'renderJobStarted',
message: {
status: 'ok',
uuid: uuid.v4()
}
});
break;
};
case 'waitingJobResults':{
console.log('waiting results of job'+msg.uuid);
break;
};
default:
break;
}
});
inbound.publish('protocol', { action: 'sendFile', message: {
file: new ArrayBuffer()
} });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment