Resource based distribution of messages
If you do SOA, you will probably get to a point where others need to be informed of changes to a certain resource. Distributor makes it easy.
// producervarDistributor=require("distributor").Distributorvardistributor=newDistributor("amqp://localhost:5672","service_name","exchange_name")// orvardistributor=newDistributor({...amqpconnectionoptions...},"service_name","exchange_name")postsResoutce=distributor.register("posts")postsResource.publish({beep: "boop"})postsResource.publish({beep: "loop"},opts,function(err){})postsResource.registerSubTopic("comments")postsResource.publishComments({name: "Bob",message: "hi"})// get your resources outwriteFile("resourceDefinitions.json",distributor.getDefinition())// consumervarClient=require("distributor").Client// fetch your resource definitionvarresourceDefintions=readFile("resourceDefinitions.json")varclient=newClient(resourceDefinitions)// want a work queue?worker=client.posts.createWorker("shared_queue_name")worker.subscribe(function(msg,cb){// subscribe on the default topic, or specify another topic (routing key)// ack when donecb()})// or pub sub instead?worker=client.posts.createSubscriber()// doesn't take a name, exclusive queue to each clientworker.subscribe("service_name.posts.comments",function(msg,cb){cb()})// want a global worker insteadworker=client.createSubscriber()// subscribes to "service_name.#" by deafultworker.subscribe(function(msg,cb){cb()})Distributor(connectionString,serviceName,exchangeName,connectionOpts)connectionString-eitheramqpconnectionString(amqp://..)oranoptionshashtobepassedtonode-amqpserviceName-thehighlevelnamefortheservice(usedwhencreatingthetopics)exchangeName-thenameoftheexchangeyouwanttouseconnectionOpts(notrequired)-extraoptionstopasstonode-amqpdistributor.registerresourceName,exchangeNameresourceName-thenameoftheresourcesthatyouwanttosendmessageson(posts,comments,etc)exchangeName(optional)-adifferentexchangetopublishtooIf you want to tweak default options/values to rabbitmq
vardistributor=require("distributor")distributor.defaults.publish.deliveryMode=1// changing ack behavior on a workerworker.onMessage=function(userHandlerFn){varself=this// don't require user to call back, auto ackreturnfunction(msg,headers,deliverInfo){userHandlerFnmsgself.queue.shift()}}Distributor defaults to reliability, which means publish and consumer confirms are enabled
Open a pull request!
MIT
