Message Queues for Everyone

Message Queues for Everyone

An intro to AMQP for Node.js developers

02cd14759ba3327f68f737131947781f?s=128

Glen Mailer

March 27, 2014
Tweet

Transcript

  1. Message queues
 for everyone An intro workshop to AMQP For

    Node.js Developers
  2. AMQP

  3. Advanced Message
 Queuing Protocol

  4. 0.9.1

  5. None
  6. Why?

  7. Job Queue
 Event Stream
 Buffered RPC

  8. Job Queue

  9. Producer Consumer

  10. Producer Consumer Producer

  11. Producer Consumer Producer Consumer

  12. Producer Consumer Producer Consumer

  13. Exchange In
 
 Queue Out

  14. Slow Operations
 Expensive Operations
 Brittle Operations

  15. 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 }
  16. Acknowledging Messages q = c.queue("task") q.subscribe(ack = TRUE, worker) worker

    = fn(body, headers, info, m) { // do work m.acknowledge() }
  17. Publishing Options Mandatory Will error if not queued Confirm Server

    acknowledges receipt
  18. Queue Declare Options Passive Don't create, get reference to existing

    queue Durable Queue still exists after a broker restart
  19. Event Stream

  20. Producer Consumer Producer Consumer Consumer Consumer Producer

  21. Messages Routing Key

  22. Exchanges Fanout: all queues
 Direct: matching key
 Topic: key patterns

  23. Exchange |
 Binding
 | Queue

  24. Topic Exchange routing.key Exact match routing.*.key routing.#.key Wildcard matching routing.key

    routing.a.key routing.b.key routing.a.b.key
  25. (soft) Real-time updates
 User notifications
 Work distribution

  26. 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")
  27. Queue Declare Options Exclusive Only one consumer on this queue

    at any time Auto Delete Queue is deleted after consumer disconnects
  28. Simulator Demo

  29. Buffered RPC

  30. Producer Consumer

  31. Smooth out Spikes
 Transparently scale
 Deferred Response

  32. Message Header Reply To
 Correlation ID

  33. Reply To reply = c.queue("abcdef") x.publish("request", headers: { replyTo: "abcdef"

    }}) reply.subscribe(handleResponse)
  34. Correlation ID x.publish("request1", headers: { replyTo: "abcdef", correlationId: "request1" })

    x.publish("request2", headers: { replyTo: "abcdef", correlationId: "request2" })
  35. Admin Interface Demo

  36. Why not just use … ?

  37. Redis pub/sub
 (Resque etc)

  38. Beanstalkd

  39. JMS
 Stomp

  40. MQTT

  41. ØMQ

  42. Node.js Quickstart

  43. npm install amqp

  44. var amqp = require('amqp')
 
 var conn = amqp.createConnection(
 {

    url: 'amqp://un:pw@host:5672/vhost' },
 { reconnect: false }
 )
 
 conn.on('ready', function() {
 // Do stuff
 })
  45. 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()
 }
  46. var opts = {
 type: 'topic',
 noDeclare: true
 }
 conn.exchange(name,

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

  48. Lets go! http://amqp-nodejs.herokuapp.com https://addons.heroku.com/marketplace/rabbitmq-bigwig

  49. @glenathan