Skip to content

zeromq/mruby-zmq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mruby-zmq

mruby bindings for https://github.com/zeromq/libzmq (v4)

Everything libzmq offers is mapped 1:1 to the LibZMQ namespace, no beautification was done. High level ruby functions are inside the ZMQ Namespace, you can find most of it inside the mrblib folder, except some of the Msg and Socket classes functions which have to be done in c.

Installation

If you dont have libzmq installed on a non Windows platform we will build it and statically link it into your mruby app. If you have it installed you need to have pkg-config installed. Then add

conf.gem mgem: 'mruby-zmq'

to your build_config.rb

Examples

Sockets

Client to Server

server = ZMQ::Server.new("tcp://127.0.0.1:*") # you can optionally add a boolean argument if it should connect instead of bind
client = ZMQ::Client.new(server.last_endpoint) # you can optionally add a boolean argument if it should bind instead of connect

client.send("hello")
msg = server.recv
puts msg['Peer-Address'] # This gets you the metadata of a message
puts msg.to_str
msg2 = ZMQ::Msg.new("hello too")
msg2.routing_id = msg.routing_id
msg2.send(server)
puts client.recv.to_str

Dealer to Router

router = ZMQ::Router.new("tcp://127.0.0.1:*")
dealer = ZMQ::Dealer.new(router.last_endpoint)

dealer.send("hello")
msg = router.recv
puts msg[1].to_str
router.send([msg[0], "hello too"])
puts dealer.recv.to_str

Pub to Sub

pub = ZMQ::Pub.new("tcp://127.0.0.1:*")
sub = ZMQ::Sub.new(pub.last_endpoint, "testme")
# you have to wait a short time here because of the slow joiner Problem
pub.send("testme hallo")
puts sub.recv.to_str

Logging

You can define a environment variable called ZMQ_LOGGER_ENDPOINT to create pub sockets which connect to that endpoint. There is also a ZMQ_LOGGER_IDENT env var which adds a ident to each msg from ZMQ.logger

ZMQ.logger.info("hallo")

Swapping out a zmq context

There is now a C API where power users can swap out a zmq context with their own,

// this shuts down the registered zmq context, closes all sockets and then terminates it.
// this must only be used on the thread which called mrb_open.
MRB_API void
mrb_zmq_ctx_shutdown_close_and_term(mrb_state* mrb);

// this swaps out the zmq context, it closes all sockets when its called for the first time after calling mrb_open.
// this must only be used on the thread which called mrb_open.
MRB_API void
mrb_zmq_set_context(mrb_state *mrb, void *context_);

This would be its declaration, to use it

#include <mruby/zmq.h>

LICENSE

Copyright 2017,2021 Hendrik Beskow

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this project, You can obtain one at http://mozilla.org/MPL/2.0/.

Contribution Policy

mruby-zmq follows the C4 Process: https://rfc.zeromq.org/spec:42/C4

About

mruby bindings for libzmq (v4)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •