jamessugrue / SwiftyZeroMQ

ZeroMQ Swift Bindings for iOS, macOS, tvOS and watchOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftyZeroMQ - ZeroMQ Swift Bindings for iOS, macOS, tvOS and watchOS

CI Status Swift ZeroMQ Platform Carthage CocoaPods License

This library provides easy-to-use iOS, macOS, tvOS and watchOS Swift bindings for the ZeroMQ C++ library. It is written in Swift 3 and features a bundled stable libzmq library. It provides ZeroMQ's low-level API along with an object-oriented API.

What is ZeroMQ?

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker. The library's API is designed to resemble that of Berkeley sockets.

Requirements

  • iOS 9+ / macOS 10.11+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.1 and Swift 3.0
  • Bitcode-enabled Xcode project for non-MacOS

Usage

Please consult the Documentation Manual for more information. Older examples can also be found in the examples github repository.

Version

import SwiftyZeroMQ

// Print ZeroMQ library and our framework version
let (major, minor, patch, versionString) = SwiftyZeroMQ.version
print("ZeroMQ library version is \(major).\(minor) with patch level .\(patch)")
print("ZeroMQ library version is \(versionString)")
print("SwiftyZeroMQ version is \(SwiftyZeroMQ.frameworkVersion)")

Request-reply Pattern

import SwiftyZeroMQ

do {
    // Define a TCP endpoint along with the text that we are going to send/recv
    let endpoint     = "tcp://127.0.0.1:5555"
    let textToBeSent = "Hello world"

    // Request socket
    let context      = try SwiftyZeroMQ.Context()
    let requestor    = try context.socket(.request)
    try requestor.connect(endpoint)

    // Reply socket
    let replier      = try context.socket(.reply)
    try replier.bind(endpoint)

    // Send it without waiting and check the reply on other socket
    try requestor.send(string: textToBeSent, options: .dontWait)
    let reply = try replier.recv()
    if reply == textToBeSent {
        print("Match")
    } else {
        print("Mismatch")
    }

} catch {
    print(error)
}

Poller

import SwiftyZeroMQ

do {
    // Define a TCP endpoint along with the text that we are going to send/recv
    let endpoint     = "tcp://127.0.0.1:5555"

    // Request socket
    let context      = try SwiftyZeroMQ.Context()
    let requestor    = try context.socket(.request)
    try requestor.connect(endpoint)

    // Reply socket
    let replier      = try context.socket(.reply)
    try replier.bind(endpoint)

    // Create a Poller and add both requestor and replier
    let poller       = SwiftyZeroMQ.Poller()
    try poller.register(socket: requestor, flags: [.pollIn, .pollOut])
    try poller.register(socket: replier, flags: [.pollIn, .pollOut])

    try requestor.send(string: "Hello replier!")

    // wait to let request come through
    sleep(1)

    var updates = try poller.poll()
    if updates[replier] == SwiftyZeroMQ.PollFlags.pollIn {
        print("Replier has data to be received.")
    }
    else {
        print("Expected replier to be in pollIn state.")
        return
    }

    try _ = replier.recv()

    updates = try poller.poll()
    if updates[replier] == SwiftyZeroMQ.PollFlags.none {
        print("All data has been received")
    }
    else {
        print("Expected replier to be in none state.")
        return
    }
} catch {
    print(error)
}

Planned Features (aka TODO)

  • More official ZeroMQ examples written
  • More ZeroMQ API wrapped

See Also

Author & License

Copyright (c) 2016-2017 Ahmad M. Zawawi under the MIT license.

A prebuilt iOS, macOS, tvOS and watchOS universal libzmq library is bundled with this library under the LGPL license.

About

ZeroMQ Swift Bindings for iOS, macOS, tvOS and watchOS

License:MIT License


Languages

Language:Swift 67.7%Language:C 27.3%Language:Ruby 4.1%Language:Objective-C 0.8%