Latest 0.1
Homepage https://github.com/travelish/SwiftNats
License MIT
Platforms ios 8.0, requires ARC
Authors

Swift client for NATS, the cloud native messaging system. https://nats.io

Install

CocoaPods

platform :ios, '8.0'
use_frameworks!

pod 'SwiftNats', '~> 0.1'

Usage

Import the package

import SwiftNats

Declare it with strong reference

var pubsub: Nats!

Basic features:

class ViewController: UIViewController, NatsDelegate {
    var pubsub: Nats!
    override func viewDidLoad() {
        super.viewDidLoad()

        pubsub = Nats(url: "nats://localhost:4222")
        pubsub.delegate = self

        //Connect
        pubsub.connect()

        //Subscribe subject
        let subject = "gossip"
        pubsub.subscribe(subject)

        //Publish
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
        dispatch_after(time, dispatch_get_main_queue()) { [weak self] in
            guard let s = self else { return }
                for index in 1...2 {
                s.pubsub.publish(subject, payload: "(index)")
            }
        }

        //Disconnect
        pubsub.disconnect()
    }

Delegate

public protocol NatsDelegate: class {
    func natsDidConnect(nats: Nats)
    func natsDidDisconnect(nats: Nats, error: NSError?)
    func natsDidReceiveMessage(nats: Nats, msg: NatsMessage)
    func natsDidReceivePing(nats: Nats)
}

Why use strong reference a.k.a property? Because of NSStreamDelegate is weak & unsafe, it does not retain the object:

unowned(unsafe) var delegate: NSStreamDelegate?

To do

  • Synchronous Messaging.
  • Request Reply.
  • Queueing.
  • Testing.

The MIT License (MIT)

Copyright (c) 2016 kakilangit

[email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Latest podspec

{
    "name": "SwiftNats",
    "version": "0.1",
    "summary": "A Swift client for the NATS messaging system.",
    "description": "Swift client for NATS, the cloud native messaging system. https://nats.io",
    "homepage": "https://github.com/travelish/SwiftNats",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "kakilangit": "[email protected]"
    },
    "social_media_url": "http://twitter.com/kakilangit",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/travelish/SwiftNats.git",
        "tag": "0.1"
    },
    "source_files": [
        "Sources",
        "Sources/**/*.{h,m}"
    ],
    "exclude_files": "Sources/Exclude",
    "requires_arc": true
}

Pin It on Pinterest

Share This