Skip to content

Instantly share code, notes, and snippets.

@trevorbernard
Created July 31, 2013 02:49
Show Gist options
  • Save trevorbernard/6118918 to your computer and use it in GitHub Desktop.
Save trevorbernard/6118918 to your computer and use it in GitHub Desktop.
Super simple way to send and receive Clojure data over ØMQ
(ns cljzmq-examples.serialization.clojure
(:require [clojure.edn :as edn]
[zeromq.zmq :as zmq]))
(defn send-clj
([socket clj-data]
(send-clj socket clj-data 0))
([socket clj-data flags]
(let [data (-> clj-data
(pr-str)
(#(.getBytes ^String %)))]
(zmq/send socket data flags))))
(defn receive-clj
([socket]
(receive-clj socket 0))
([socket flags]
(-> (zmq/receive socket flags)
(#(String. ^bytes %))
(edn/read-string))))
(ns cljzmq-examples.serialization.clojure-test
(:require [clojure.test :refer :all]
[cljzmq-examples.serialization.clojure :refer :all]
[zeromq.zmq :as zmq]))
(deftest clojure-serialization-test
(let [context (zmq/zcontext)]
(with-open [receiver (doto (zmq/socket context :pull)
(zmq/bind "tcp://*:8765"))
sender (doto (zmq/socket context :push)
(zmq/connect "tcp://127.0.0.1:8765"))]
(send-clj sender {:msg "Hello, World!"})
(let [actual (receive-clj receiver)]
(is (= {:msg "Hello, World!"} actual))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment