Coder Social home page Coder Social logo

jbatra-umeey / cocoamqtt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from emqx/cocoamqtt

0.0 0.0 0.0 1.27 MB

MQTT for iOS and macOS written with Swift

Home Page: https://www.emqx.io

License: Other

Swift 98.86% Ruby 0.74% Objective-C 0.32% Shell 0.08%

cocoamqtt's Introduction

CocoaMQTT

PodVersion Platforms License Swift version Coverage Status

MQTT v3.1.1 client library for iOS/macOS/tvOS written with Swift 5

Build

Build with Xcode 11.1 / Swift 5.1

iOS Target: 10.0 or above

Installation

CocoaPods

Install using CocoaPods by adding this line to your Podfile:

use_frameworks! # Add this if you are targeting iOS 8+ or using Swift
pod 'CocoaMQTT', '1.3.0-rc.2'

Then, run the following command:

$ pod install

Carthage

Install using Carthage by adding the following lines to your Cartfile:

github "emqx/CocoaMQTT" "master"

Then, run the following command:

$ carthage update --platform iOS,macOS,tvOS

Last if you're building for OS X:

  • On your application targets “General” settings tab, in the "Embedded Binaries" section, drag and drop CocoaMQTT.framework from the Carthage/Build/Mac folder on disk.

If you're building for iOS, tvOS:

  • On your application targets “General” settings tab, in the "Linked Frameworks and Libraries" section, drag and drop each framework you want to use from the Carthage/Build folder on disk.

  • On your application targets "Build Phases" settings tab, click the "+" icon and choose "New Run Script Phase". Create a Run Script with the following contents:

    /usr/local/bin/carthage copy-frameworks
    
  • and add the paths to the frameworks you want to use under "Input Files", e.g.:

    $(SRCROOT)/Carthage/Build/iOS/CocoaMQTT.framework
    

Usage

Create a client to connect MQTT broker:

let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 60
mqtt.delegate = self
mqtt.connect()

Now you can use closures instead of CocoaMQTTDelegate:

mqtt.didReceiveMessage = { mqtt, message, id in
	print("Message received in topic \(message.topic) with payload \(message.string!)")           
}

SSL Secure

One-way certification

No certificate is required locally. If you want to trust all untrust CA certificates, you can do this:

mqtt.allowUntrustCACertificate = true

Two-way certification

Need a .p12 file which is generated by a public key file and a private key file. You can generate the p12 file in the terminal:

openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12

MQTT over Websocket

In the 1.3.0, The CocoaMQTT has supported to connect to MQTT Broker by Websocket.

If you integrated by CocoaPods, you need to modify you Podfile like the followings and execute pod install again:

use_frameworks!

target 'Example' do
    pod 'CocoaMQTT/WebSockets', '1.3.0-rc.2'
end

If you're using CocoaMQTT in a project with only a .podspec and no Podfile, e.g. in a module for React Native, add this line to your .podspec:

Pod::Spec.new do |s|
  ...
  s.dependency "Starscream", "~> 3.1.1"
end

Then, Create a MQTT instance over Websocket:

let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)

// ...

_ = mqtt.connect()

If you want to add additional custom header to the connection, you can use the following:

let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
websocket.headers = [
            "x-api-key": "value"
        ]
        websocket.enableSSL = true

let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)

// ...

_ = mqtt.connect()

Example App

You can follow the Example App to learn how to use it. But we need to make the Example App works fisrt:

$ cd Examples

$ pod install

Then, open the Example.xcworkspace/ by Xcode and start it!

Dependencies

These third-party functions are used:

LICENSE

MIT License (see LICENSE)

Contributors

Author

Twitter

https://twitter.com/emqtt

cocoamqtt's People

Contributors

hjianbo avatar alexyu1989 avatar filippocamillotexa avatar amsibsam avatar cyrusingraham avatar andypiper avatar manucheri avatar hsoi avatar karinberg avatar michalsrutek avatar leeway1208 avatar stansidel avatar petrpavlik avatar mindaugasjucius avatar massi-ang avatar adicatur avatar xandrefreire avatar wwgilpin avatar rycai-decoda avatar schraeer avatar sachinvas avatar pavoni avatar nauyihs avatar benji-bou avatar theyigit avatar sotpt avatar orbitbot avatar ptescher avatar mcanlatch avatar lucakaufmann avatar

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.