hxzmq: ZeroMQ binding for Haxe

Welcome to hxzmq, Haxe language bindings for the ZeroMQ messaging library. It is written and maintained by Richard Smith of RSBA Technology Ltd

ZeroMQ (shortened to “0MQ”) is a lightweight, fast messaging C library that implements several different message topologies across different transports, both with brokers or “brokerless” designs, or any interesting combination of the two. It has community – provided bindings for over 15 application languages (including Haxe!) and has been compiled to run on Windows, Linux, iOS and Mac0SX operating systems, amongst others. 0MQ is maintained as an open source project by iMatix Corporation and a strong development community, and is licensed under LGPL.

A Quick Example


This shows a simple filter device that collects data from two different remote publishers and sends it local subscribers:
package ;
import haxe.io.Bytes;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMsg;

class Example 
{
    public static function main() {
        var context = new ZContext();
        var subscriber = context.createSocket(ZMQ_SUB);
        subscriber.connect("tcp://192.168.55.112:5556");
        subscriber.connect("tcp://192.168.55.201:7721");
        subscriber.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("NASDAQ"));
        
        var publisher = context.createSocket(ZMQ_PUB);
        publisher.bind("ipc://nasdaq-feed");
        
        while (true) {
            var message = ZMsg.recvMsg(subscriber);
            message.send(publisher);
        }
    }
}

Ensure that libzmq is installed on your development machine (see: http://www.zeromq.org/intro:get-the-software)

If you want to use the php target, you must also install the php-zmq 0MQ binding (see http://www.zeromq.org/bindings:php)

Then install hxzmq from haxelib (or install from source):

haxelib install hxzmq

Compile & run for your chosen target platform, eg:

(For cpp on Mac0SX or Linux):

haxe –lib hxzmq -D HXCPP_M64 –cpp out/Mac64 –cp . –main Example
cd out/Mac64
./Example

(For neko on Windows):

haxe –lib hxzmq –neko out/Windows/main.n –cp . –main Example
cd out/Windows
neko main.n

(for php on any platform, running using PHP CLI):

haxe –lib hxzmq –php out –cp . –main Example
cd out
php –f index.php

About ZeroMQ

0MQ is a relatively recent entrant into the messaging layer software space, conceived as a low-level, cross-platform but highly - performant messaging library that can replace direct use of in-process and tcp sockets etc with a message-orientated API. It implements a number of well-defined message patterns via a number of 0MQ socket types that each have a well-defined behaviour and usage patterns. Combining 0MQ sockets enables highly scalable messaging topologies to be implemented. 0MQ sockets maybe connected to multiple endpoints while simultaneously accepting incoming connections from multiple endpoints bound to the same socket, thus allowing many-to-many relationships.

Current ZeroMQ socket types (see http://api.zeromq.org “zmq_socket” for more details)

  • Asynchronous Request – Reply (ZMQ_REQ and ZMQ_REP socket types, extendable using ZMQ_ROUTER and ZMQ_DEALER distribution sockets)
  • Publish – Subscribe (ZMQ_PUB and ZMQ_SUB socket types)
  • Pipeline (ZMQ_PUSH and ZMQ_PULL)
  • Exclusive Pair (ZMQ_PAIR)

The active 0MQ community is centered around the zeromq.org website. Pieter Hintjens has written an excellent ZeroMQ Guide which acts as an extended tutorial on ZeroMQ best practise and usage, with 100s of code examples in different languages (including Haxe).

Introduction to hxzmq

The hxzmq project provides a Haxe language binding for ZeroMQ, currently supporting (for hxzmq v1.3.0 at the time of writing) the Haxe cpp, neko and php compilation targets.

Combining Haxe with ZeroMQ allows developers to create system architectures with any combination of those targets across multiple operating systems using ZeroMQ as the messaging layer. For example:

  • Build a Haxe PHP web service that communicates with a distributed Haxe cpp backend system using ZeroMQ
  • Create a Haxe multi-threaded application using ZeroMQ ipc transport messaging (allowing later refactoring of threads into separate processes / components).
  • Create a Haxe neko Mongrel2 ZeroMQ handler service for fast AJAX client-server communication (+ using Haxe javascript on the web frontend wrapping the Websocket API etc)

Because 0MQ has so many other language bindings, you can combine Haxe – written components with 0MQ – enabled components written in Java, C++, C#, Ruby etc into the same distributed system.

The hxzmq project includes several layers of software:

  • hxzmq.ndll
    A compiled C shared library file that bridges the native libzmq API to Haxe neko or CPP applications using the CFFI foreign function interface of the hxcpp Haxe library.
  • 0MQ API Haxe wrapper classes (org.zeromq package)
    The ZMQxxxxx classes in the org.zeromq package provide an API close to the original libzmq C implementation, for managing ZMQ contexts (ZMQContext.hx), sockets (ZMQSocket.hx), pollers (ZMQPoller.hx) and constants (ZMQ.hx) etc. These classes wrap the calls to the underlying hxzmq.ndll or php-zmq libraries / modules.
  • High – level Haxe API (org.zeromq package)
    The Zxxxxx classes in the org.zeromq package provide a higher-level API that allow developers to work with managed contexts (ZContext.hx), message frames (ZFrame.hx) and multi-part messages (ZMsg.hx), a reactor loop implementation (ZLoop.hx) and so on. These were inspired by the czmq C language binding by iMatix.
  • Haxe remoting (org.zeromq.remoting)
    The classes in the org.zeromq.remoting package allow Haxe remoting procedure calls between 0MQ sockets (e.g. using a Request-Reply topology) instead of raw tcp. This enables complex distributed architectures to be built with Haxe remoting used at the endpoints, with intermediate 0MQ devices in the middle (which can also be written in Haxe).
version #10755, modified 2011-07-19 12:50:57 by rjsmith