Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Message Queues for Everyone

Message Queues for Everyone

An intro to AMQP for Node.js developers

Glen Mailer

March 27, 2014
Tweet

More Decks by Glen Mailer

Other Decks in Technology

Transcript

  1. Producer c = create_channel q = c.queue("task") x = c.default_exchange

    x.publish( "job1", "payload" ) Consumer c = create_channel q = c.queue("task") q.subscribe(worker) worker = fn(*message) { // do stuff }
  2. Acknowledging Messages q = c.queue("task") q.subscribe(ack = TRUE, worker) worker

    = fn(body, headers, info, m) { // do work m.acknowledge() }
  3. Queue Declare Options Passive Don't create, get reference to existing

    queue Durable Queue still exists after a broker restart
  4. Binding a Queue x = c.exchange("activity”) q = c.queue("alert") q.bind(x,

    "balance.*") q.bind(x, "transfer.out") x.publish("balance.low") x.publish("transfer.in") x.publish("transfer.out")
  5. Queue Declare Options Exclusive Only one consumer on this queue

    at any time Auto Delete Queue is deleted after consumer disconnects
  6. Correlation ID x.publish("request1", headers: { replyTo: "abcdef", correlationId: "request1" })

    x.publish("request2", headers: { replyTo: "abcdef", correlationId: "request2" })
  7. var amqp = require('amqp')
 
 var conn = amqp.createConnection(
 {

    url: 'amqp://un:pw@host:5672/vhost' },
 { reconnect: false }
 )
 
 conn.on('ready', function() {
 // Do stuff
 })
  8. var opts = { noDeclare: true }
 conn.queue(name, opts, function(q)

    {
 q.subscribe(
 { ack: true, prefetch: 1 },
 onMessage
 )
 })
 function onMessage(body, meta, info, msg) {
 // do some stuff
 msg.acknowledge()
 }
  9. var opts = {
 type: 'topic',
 noDeclare: true
 }
 conn.exchange(name,

    opts, function(x) {
 x.publish('key', 'body', {
 contentType: 'text/json'
 })
 })