Skip to content

Instantly share code, notes, and snippets.

View thealmikey's full-sized avatar
🎯
Focusing

Michael Gikaru thealmikey

🎯
Focusing
View GitHub Profile
socket.send(rawReply, 0)
import org.zeromq.ZMQ
fun main() {
val context = ZMQ.context(1)
val socket = context.socket(ZMQ.REP)
println("Starting the server...")
socket.bind("tcp://*:5897")
val byteReply = socket.recv(0)
var plainReply = String(byteReply, 0, byteReply.size - 1)
println("Received reply $plainReply")
}
org.zeromq.ZMQ
fun main() {
val context = ZMQ.context(1)
val socket = context.socket(ZMQ.REQ)
println("connecting to hello world server...")
socket.connect("tcp://localhost:5897")
import org.zeromq.ZMQ
fun main() {
val context = ZMQ.context(1)
val socket = context.socket(ZMQ.REQ)
println("connecting to hello world server...")
socket.connect("tcp://localhost:5897")
for (i in 1..10) {
var plainRequest = "Hello "
var byteRequest = plainRequest.toByteArray()
byteRequest[byteRequest.size - 1] = 0
println("sending request $i $plainRequest")
socket.send(byteRequest, 0)

ZeroMQ is described on wikipedia as a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications.

It has features of a networking library and touches a lot on concurrency, making it a bit difficult to categorize. I think one can define it using the utility to which it serves the person using it, the authors of the library do it more justice in their definition on their site in a hundred words.

The library has ports to many languages and the site has plenty of examples. For learning purposes i wanted to explore how to implement it in Kotlin which am learning as well. Fortunately for us ZeroMQ has a nice port in Java called JeroMQ

We'll create a basic example that creates a server and client using Kotlin. We begin by creating a kotlin Gradle project using our IDE

We'll import JeroMQ from maven repository using

part2
import org.zeromq.ZMQ
fun main() {
val context = ZMQ.context(1)
val socket = context.socket(ZMQ.PULL)
println("Starting the puller...")
part2
import org.zeromq.ZMQ
fun main() {
val context = ZMQ.context(1)
val socket = context.socket(ZMQ.PUSH)
println("connecting to a pulling client...")
socket.connect("tcp://localhost:5897")

In part 1 we saw how to create a simple server using Reply and Request pattern. We mentioned in passing other kinds of sockets we can make using ZeroMQ. We will cover a simple example of a Push and Pull pattern using zmq.PULL and ZMQ.PUSH and also cover one for Publish and Subscribe with ZMQ.PUB and ZMQ.SUB. The content will be very similar to the one from the previous tutorial and we will just tweak a few values to have the example up and running.

The basic idea with Publish coupled with Subscribe and Push with Pull is all about the flow of information, where we have one or more nodes Producing/Pushing out information for example a temperature sensor sending out data, a smart-wear gadget sending health data say like hearbeats from a smart watch, a drone or a even the self-driving car you might be designing(who knows you might be the next Elon Musk). On the other end we have a node Subscribing/Pulling information from a source for example a data center, heating system reacting to the temperature change and t