Now that you have ZeroMQ working with Clojure, some introductory examples are in order. The demonstrations below show how simple it is to bridge the gap between programming languages by using ZeroMQ, focusing on connecting Clojure and Ruby.
##Pub/Sub Pattern
Our first example is the pub/sub pattern. Our code will broadcast messages from Ruby which our Clojure process can subscribe to. One interesting capability of 0MQ is that we can subscribe to “channels” — that is, we’ll only receive messages which start with a given query.1
After launching the Clojure process, hop into the shell:2
##Request/Response Pattern
The request/response technique allows us to delegate work from our master program to a worker process. In this particular example, we’re sending lists of values from our Ruby process to our Clojure process, which adds them and sends back the result.
Fire up the Clojure process, and hop back into the shell:
This could be useful for using 0MQ for method dispatch, e.g. this socket will accept “add” messages and sum the terms in a message, whereas another socket will accept “log” messages and log the message to file. ↩
If you launched the process through lein and swank-clojure, your output will appear in the console which started lein swank. ↩