Skip to content

Instantly share code, notes, and snippets.

@dudleycarr
Created February 18, 2015 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudleycarr/8f9c04e63f599b222b41 to your computer and use it in GitHub Desktop.
Save dudleycarr/8f9c04e63f599b222b41 to your computer and use it in GitHub Desktop.
Timing nsqjs messages
nsq = require 'nsqjs'
TOPIC = 'test_topic'
# Publish 10 messages
writer = new nsq.Writer '127.0.0.1', 4150
writer.connect()
writer.on 'ready', ->
writer.publish TOPIC, ("#{i}" for i in [0...10])
# Just for the purposes of making the output more legible.
shortId = (id) ->
id[-5..]
reader = new nsq.Reader TOPIC, 'default_channel',
maxInFlight: 5
nsqdTCPAddresses: ['127.0.0.1:4150']
# Message timing
reader.on 'message', (msg) ->
console.time shortId msg.id
msg.on 'respond', (responseCode) ->
if responseCode in [0,1]
console.timeEnd shortId msg.id
# Normal message handler
reader.on 'message' , (msg) ->
delay = Math.floor(Math.random() * 10) + 1
console.log "#{shortId msg.id} processing for #{delay}s"
done = ->
msg.finish()
setTimeout done, delay * 1000
reader.connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment