GithubHelp home page GithubHelp logo

hhy5277 / mqttkit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mobile-web-messaging/mqttkit

0.0 1.0 0.0 517 KB

MQTT Objective-C client for iOS

License: Apache License 2.0

Ruby 0.30% Objective-C 11.71% C 87.87% C++ 0.12%

mqttkit's Introduction

MQTTKit

MQTTKit is a modern event-driven Objective-C library for MQTT 3.1.

It uses Mosquitto 1.2.3 library.

An iOS application using MQTTKit is available at MQTTExample.

Project Status

This project is no longer maintained (some context about this decision).

If you encounter bugs with it or need enhancements, you can fork it and modify it as the project is under the Apache License 2.0.

Build Status

Installation Using CocoaPods

On your Podfile add this project:

...
pod 'MQTTKit', :git => 'https://github.com/mobile-web-messaging/MQTTKit.git'
...

For the first time, run pod install, if you are updating the project invoke pod update.

Usage

Import the MQTTKit.h header file

#import <MQTTKit.h>

Send a Message

// create the client with a unique client ID
NSString *clientID = ...
MQTTClient *client = [[MQTTClient alloc] initWithClientId:clientID];

// connect to the MQTT server
[self.client connectToHost:@"iot.eclipse.org" 
         completionHandler:^(NSUInteger code) {
    if (code == ConnectionAccepted) {
        // when the client is connected, send a MQTT message
        [self.client publishString:@"Hello, MQTT"
                           toTopic:@"/MQTTKit/example"
                           withQos:AtMostOnce
                            retain:NO
                 completionHandler:^(int mid) {
            NSLog(@"message has been delivered");
        }];
    }
}];

Subscribe to a Topic and Receive Messages

// define the handler that will be called when MQTT messages are received by the client
[self.client setMessageHandler:^(MQTTMessage *message) {
    NSString *text = [message.payloadString];
    NSLog(@"received message %@", text);
}];

// connect the MQTT client
[self.client connectToHost:@"iot.eclipse.org"
         completionHandler:^(MQTTConnectionReturnCode code) {
    if (code == ConnectionAccepted) {
        // when the client is connected, subscribe to the topic to receive message.
        [self.client subscribe:@"/MQTTKit/example"
         withCompletionHandler:nil];
    }
}];

Disconnect from the server

[self.client disconnectWithCompletionHandler:^(NSUInteger code) {
    // The client is disconnected when this completion handler is called
    NSLog(@"MQTT client is disconnected");
}];

Authors

mqttkit's People

Contributors

dbburgess avatar jmesnil avatar lubbo avatar scapegoat4u avatar theojulienne avatar

Watchers

 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.