Giter Site home page Giter Site logo

nanomessage's Introduction

NanoMessage

Swift Swift Build Status macOS Linux License

NanoMessage is a Swift-3/4 class wrapper of nanomsg

Socket Protocols

  • Pair
  • Push/Pull
  • Request/Reply
  • Publisher/Subscriber
  • Surveyor/Respondent
  • Bus

Transport Mechanisms

  • TCP/IP
  • In-Process Communication
  • Inter-Process Communication
  • Web Socket

Features

  • Send/Receive (Synchronous/Asynchronous) Blocking/Non-Blocking And Timeout
  • Same message payload object for Send/Receive with message context
  • Socket Polling (Single or Multiple)
  • Device/Socket Binding (Synchronous/Asynchronous)
  • Socket Loopback (Synchronous/Asynchronous)

Usage

If Swift Package Manager is used, add this package as a dependency in Package.swift,

import PackageDescription

let package = Package (
    name:  "<your-app-name>",
    dependencies: [
        .Package(url: "https://github.com/itssofluffy/NanoMessage.git", majorVersion: 0)
    ]
)

Examples

Push

import Foundation
import NanoMessage

do {
    guard let url = URL(string: "tcp://localhost:5555") else {
        fatalError("url is not valid")
    }

    let node = try PushSocket()

    let _: Int = try node.createEndPoint(url: url, type: .Connect)

    try node.sendMessage(Message(value: "This is earth calling...earth calling..."),
                         timeout: TimeInterval(seconds: 10))
} catch let error as NanoMessageError {
    print(error)
} catch {
    print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}

Pull

import Foundation
import NanoMessage

do {
    guard let url = URL(string: "tcp://*:5555") else {
        fatalError("url is not valid")
    }

    let node = try PullSocket()

    let endPoint: EndPoint = try node.createEndPoint(url: url, type: .Bind, name: "my local end-point")

    let received = try node.receiveMessage(timeout: TimeInterval(seconds: 10))

    print("bytes  : \(received.bytes)")            // 40
    print("message: \(received.message.string)")   // This is earth calling...earth calling...

    if (try node.removeEndPoint(endPoint)) {
        print("end-point removed")
    }
} catch let error as NanoMessageError {
    print(error)
} catch {
    print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}

Request

import Foundation
import NanoMessage

do {
    guard let url = URL(string: "tcp://localhost:5555") else {
        fatalError("url is not valid")
    }

    let node = try RequestSocket()

    let endPoint: EndPoint = try node.createEndPoint(url: url, type: .Connect, name: "request end-point")

    usleep(TimeInterval(milliseconds: 200))

    let timeout = TimeInterval(seconds: 10)
    let message = Message(value: "ping")

    let received = try node.sendMessage(message, sendTimeout: timeout, receiveTimeout: timeout) { sent in
        print("sent \(sent)")
    }

    print("and recieved \(received)")
} catch let error as NanoMessageError {
    print(error)
} catch {
    print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}

Reply

import Foundation
import NanoMessage

do {
    guard let url = URL(string: "tcp://*:5555") else {
        fatalError("url is not valid")
    }

    let node = try ReplySocket()

    let endPoint: EndPoint = try node.createEndPoint(url: url, type: .Bind, name: "reply end-point")

    usleep(TimeInterval(milliseconds: 200))

    let timeout = TimeInterval(seconds: 10)

    let sent = try node.receiveMessage(receiveTimeout: timeout, sendTimeout: timeout) { received in
        print("received \(received)")

        var message = ""

        if (received.message.string == "ping") {
            message = "pong"
        } else {
            message = "personally i prefer wiff-wafe"
        }

        return Message(value: message)
    }

    print("and sent \(sent)")
} catch let error as NanoMessageError {
    print(error)
} catch {
    print("an unexpected error '\(error)' has occured in the library libNanoMessage.")
}

License

This project is released under the MIT license. See LICENSE for details.

nanomessage's People

Contributors

itssofluffy avatar taborkelly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

redsift jbinkleyj

nanomessage's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.