Provides an http interface to nsq consumers.
NOTE: This has not updated since before nsqio/pynsq#117 "nsq.Reader.disabled() no longer effective" was fixed.
nsqd
already provides an http interface to nsq publishers - just
POST data to /put?topic=$TOPIC
, typically to the nsqd running locally,
and nsqd will take care of delivery to consumers including timeouts and
retries, and persisting messages that overflow the in-memory queue to disk.
But nsqd provides only its own tcp protocol interface to consumers, for two major reasons:
- nsq consumers are pushed messages, consumers do not poll
- nsq consumers should process a message, and then mark it finished
This little server process allows consumers to poll or long-poll with http, and automatically marks messages as finished so consumers don't have to. So this is less efficient and less safe than a real nsq consumer library, but probably OK and more convenient for some use cases.
Requires python 2.7, and the "tornado" and "pynsq" python modules.
Run with the --help
argument to see possible arguments.
At least --nsqd_tcp_addresses=...
or --lookupd_http_addresses=...
is required.
(They take one or more comma-separated "host:port").
There is only one endpoint: /get
, with the following parameters:
- topic: nsq topic
- channel: nsq channel
- timeout (optional): set default
Example which long-polls for a message:
curl 'http://127.0.0.1:4159/get?topic=test&channel=testc&timeout=20'
The default timeout is 1 second (the default can be changed with a command-line argument). The shortest possible poll can't be too short, perhaps 100ms, due to the push/poll conversion. Currently, the delay has second granularity.
Useful shell commands for testing with curl
Publishing 10 json messages to nsqd:
for X in $(seq 1 10) ; do curl --data "{\"json\": \"message$X\"}" 'http://127.0.0.1:4151/put?topic=test' ; echo ; done
Requesting 4 messages from nsq_http_consumer in parallel:
for X in $(seq 4) ; do (OUTPUT=$(curl -s 'http://dockerdev:4159/get?topic=test&channel=testc' 2>&1) ; echo "$OUTPUT") & done ; sleep 2