Skip to content

Instantly share code, notes, and snippets.

@meoyawn
Last active April 14, 2019 10:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoyawn/037f25f9a12a263216c42edf66c97409 to your computer and use it in GitHub Desktop.
Save meoyawn/037f25f9a12a263216c42edf66c97409 to your computer and use it in GitHub Desktop.
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