Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CLI instant messenger in 60 lines with ZMQ
package chat
import org.zeromq.ZMQ
import kotlin.concurrent.thread
fun main(args: Array<String>): Unit =
ZMQ.context(1).use { ctx ->
thread {
ctx.socket(ZMQ.SUB).use { stream ->
stream.connect(MESSAGE_STREAM)
stream.subscribe(ByteArray(0))
while (true) {
val msg = stream.recvStr()
println("client got $msg")
}
}
}
ctx.socket(ZMQ.PUSH).use { push ->
push.connect(POST_MESSAGE)
while (true) {
val msg = readLine()
println("will send $msg")
push.send(msg)
}
}
}
package chat
val POST_MESSAGE = "tcp://localhost:6573"
val MESSAGE_STREAM = "tcp://localhost:6574"
package chat
import org.zeromq.ZMQ
fun main(args: Array<String>): Unit =
ZMQ.context(1).use { ctx ->
ctx.socket(ZMQ.PULL).use { post ->
post.bind(POST_MESSAGE)
ctx.socket(ZMQ.PUB).use { stream ->
stream.bind(MESSAGE_STREAM)
while (true) {
val msg = post.recvStr()
println("server got $msg")
stream.send(msg)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment