Skip to content

rjsmith/hxzmq

Repository files navigation

hxzmq - haXe Language Bindings for ZeroMQ

Welcome to hxzmq, [haXe language bindings] 2 for the iMatix [ZeroMQ messaging library] 3. 0MQ is a lightweight and fast messaging implementation.

By Richard Smith [RSBA Technology Ltd] 1

Introduction

This repository provides C++ binding code that wraps the libzmq library API to create a Neko DLL file, hxzmq.ndll. The ndll is then accessed via the hxzmq org.zeromq package to expose the 0MQ API to haXe application code targetted at C++ or nekovm platforms.

Also included is wrapper code around the [existing PHP 0MQ binding] 12, enabling haXe programs compiled for the PHP environment can also make use of 0MQ socket technology.

Background & Rationale

haXe enables applications to be written in a single unified programming language that can then be executed on any combination of an ever-growing number of [target language platforms.] 6. It is quite possible to write back-end server code targetted at php or C++, with a rich internet application Flash or javascript front-end, plus an iPhone application (via the C++ target), all using a single shared haXe codebase. Code written using non-target specific APIs can be automatically re-used on any of these platforms, such as an application's internal domain model or framework code. Conditional compilation, together with many target - specific APIs contained in the [haXe standard library] 7, provides the opportunity to access platform-specific features, giving the best of both worlds. Most of the target platforms also support extending the standard capabilities by use of externs and Foreign Function Interface mechanisms; an ability which has been used to write hxzmq. haXe is an [open source project] 7.

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 (e.g. Request-Reply, Publish-Subscribe) that allow very complex distributed system architectures to be built from simple parts. 0MQ is maintained as an [open source project] 8 by iMatix.

hxzmq has been written to allow the author (and anyone else who might be interested) to explore and experiment what improvements in software design practise can be made when a cross-platform language is coupled with a highly performant cross-platform messaging layer.

haXe Code Example

Client:

package ;
import haxe.io.Bytes;
import org.zeromq.ZMQ;
import org.zeromq.ZMQContext;
import org.zeromq.ZMQSocket;

/**
 * Hello World client in Haxe.
 */
class HelloWorldClient 
{
	public static function main() {
		var context:ZMQContext = ZMQContext.instance();
		var socket:ZMQSocket = context.socket(ZMQ_REQ);
		socket.connect ("tcp://localhost:5556");
		for (i in 0...10) {
			var requestString = "Hello ";
			socket.sendMsg(Bytes.ofString(requestString));
			var msg:Bytes = socket.recvMsg();
			trace ("Received reply " + i + ": [" + msg.toString() + "]");				
		}
		socket.close();
		context.term();
	}
}

Server:

package ;
import haxe.io.Bytes;
import org.zeromq.ZMQ;
import org.zeromq.ZMQContext;
import org.zeromq.ZMQException;
import org.zeromq.ZMQSocket;

/**
 * Hello World server in Haxe
 * Binds REP to tcp://*:5556
 * Expects "Hello" from client, replies with "World"
 * 
 */
class HelloWorldServer 
{
	public static function main() {			
		var context:ZMQContext = ZMQContext.instance();
		var responder:ZMQSocket = context.socket(ZMQ_REP);
		responder.bind("tcp://*:5556");			
		while (true) {
			var request:Bytes = responder.recvMsg();
			// Do some work
			Sys.sleep(1);
			// Send reply back to client
			responder.sendMsg(Bytes.ofString("World"));
		}
		responder.close();
		context.term();	
	}
}

Contents

Key files and folders contained in this repository:

  • build.xml

    XML compilation configuration file used by the hxcpp cross-platform ndll build tool to compile & build the hxzmq.ndll library. See INSTALL.md for further details.

  • buildmac64.sh

    Mac OSX 64bit build shell script that builds hxzmq.ndll, unit test and guide programs

  • buildlinux.sh

    Linux 32bit build shell script that builds hxzmq.ndll, unit test and guide programs

  • build.bat

    Windows script file that builds hxzmq.ndll, unit test and guide programs

  • /src

    The C++ code that wraps the libzmq C library in [hxcpp CFFI] 10 calls, which exposes it to the haXe layer. Compiles into the hxzmq.ndll library.

  • /org/zeromq

    The haXe code that invokes the native functions defined in hxzmq.ndll. Provides the core API used by haXe applications (ZMQxxxxx.hx files) and higher-level API classes (Zxxxx.hx files).

  • /org/zeromq/remoting

    haXe classes that implement a haXe remoting wrapper on top of ZMQSocket objects.

  • /org/zeromq/test

    Unit tests for the org.zeromq package. Main program invoked from the TestAll.hx class.

  • /test

    Contains build hxml files for compiling the unit tests on different platforms.

  • ndll/

    Contains pre-built ndll files for different platforms

  • doc/

    Contains generated HTML documentation for the ZMQxxx.hx and Zxxx.hx class files.

Versions

The current release of hxzmq on the master branch is compatable with libzmq-3.1.0 or any later 3.1.x version (latest tested in 3.1.0). The latest released hxzmq package shall also be available in the [haxelib repository] 4, accessable via the [haxelib tool] 5 which is included in the standard haXe distribution.

A version of hxzmq compatable with the libzmq 2.1.x version is archived on the 2.1.x branch of the hxzmq git repository.

Building and Installation

If you are a haXe user who just wants to start using 0MQ and hxzmq in your projects, make sure libzmq.dll is available on your system's PATH setting, and then simply install the latest hxzmq package available from the haXe repository, using the haxelib tool:

haxelib install hxzmq

Please refer to the separate INSTALL.md file in this distribution for details on how to build and install hxzmq.ndll from source.

Copying

Free use of this software is granted under the terms of the GNU Lesser General Public License (LGPL). For details see the files COPYING.LESSER included with the hxzmq distribution.

ZGuide

Further [0MQ Haxe examples] 14 have been added to the [0MQ Guide] 11. These work through many messaging topology examples using haXe code.

About

Haxe bindings for ZeroMQ

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages