Giter Site home page Giter Site logo

luna-moonfang / cocoamqtt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from emqx/cocoamqtt

0.0 0.0 0.0 1.29 MB

MQTT 5.0 client library for iOS and macOS written in Swift

Home Page: https://www.emqx.com/en

License: Other

Shell 0.04% Ruby 0.47% Objective-C 0.16% Swift 99.32%

cocoamqtt's Introduction

CocoaMQTT

PodVersion Platforms License Swift version

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

Build

Build with Xcode 11.1 / Swift 5.1

IOS Target: 12.0 or above OSX Target: 10.13 or above TVOS Target: 10.0 or above

xcode 14.3 issue:

File not found: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphonesimulator.a

If you encounter the issue, Please update your project minimum depolyments to 11.0

Installation

CocoaPods

To integrate CocoaMQTT into your Xcode project using CocoaPods, you need to modify you Podfile like the followings:

use_frameworks!

target 'Example' do
    pod 'CocoaMQTT'
end

Then, run the following command:

$ pod install

At last, import "CocoaMQTT" to your project:

import CocoaMQTT

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 --use-xcframeworks

At last:

On your application targets “General” settings tab, in the "Frameworks, Libraries, and Embedded content" section, drag and drop CocoaMQTT.xcframework, CocoaAsyncSocket.xcframework and Starscream.xcframework from the Carthage/Build folder on disk. Then select "Embed & Sign".

Usage

Create a client to connect MQTT broker:

///MQTT 5.0
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt5 = CocoaMQTT5(clientID: clientID, host: "broker.emqx.io", port: 1883)

let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
connectProperties.sessionExpiryInterval = 0
connectProperties.receiveMaximum = 100
connectProperties.maximumPacketSize = 500
mqtt5.connectProperties = connectProperties

mqtt5.username = "test"
mqtt5.password = "public"
mqtt5.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout")
mqtt5.keepAlive = 60
mqtt5.delegate = self
mqtt5.connect()

///MQTT 3.1.1
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "broker.emqx.io", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTMessage(topic: "/will", string: "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

Note: Please use openssl version 1.1 (e.g. brew install [email protected]), otherwise you may not be able to import the generated .p12 file to the system correctly.

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'
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"
end

Then, Create a MQTT instance over Websocket:

///MQTT 5.0
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt5 = CocoaMQTT5(clientID: clientID, host: host, port: 8083, socket: websocket)
let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
// ...
mqtt5.connectProperties = connectProperties
// ...

_ = mqtt5.connect()

///MQTT 3.1.1
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:

GCDAsyncSocket

LICENSE

MIT License (see LICENSE)

Contributors

Author

Twitter

https://twitter.com/EMQTech

cocoamqtt's People

Contributors

1989allen126 avatar adicatur avatar alexyu1989 avatar amsibsam avatar andypiper avatar cyrusingraham avatar dahuayuan avatar filippocamillotexa avatar hjianbo avatar hsoi avatar karinberg avatar leeway1208 avatar manucheri avatar massi-ang avatar mcanlatch avatar michalsrutek avatar mindaugasjucius avatar omkar-sharechat avatar orbitbot avatar petrpavlik avatar przemyslaw-szurmak avatar ptescher avatar sotpt avatar spinnerok avatar stansidel avatar swilder-m avatar theyigit avatar tynox avatar xandrefreire avatar zhihuitang 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.