Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
mgdm edited this page Jan 2, 2015 · 2 revisions

''mosquitto-php'' let you use the Mosquitto MQTT library with PHP.

See https://github.com/mgdm/Mosquitto-PHP for the full API documentation.

Publishing to a topic

<?php
define('BROKER', 'localhost');
define('PORT', 1883);
define('CLIENT_ID', "pubclient_" + getmypid());
$client = new Mosquitto\Client(CLIENT_ID);
$client->connect(BROKER, PORT, 60);
while ($client->loop() == 0) {
	$message = "Test message at " . date("Y-m-d H:i:s");
	$client->publish('test/foo/bar', $message, 0, false);
	$client->loop();
	sleep(1);
}

Subscribing to a topic

All the callbacks here are actual functions, though they can be closures, methods, etc.

<?php
define('BROKER', 'localhost');
define('PORT', 1883);
define('CLIENT_ID', "pubclient_" + getmypid());
$client = new Mosquitto\Client(CLIENT_ID);
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect(BROKER, PORT, 60);
$client->subscribe('#', 1); // Subscribe to all messages
$client->loopForever();
function connect($r) {
	echo "Received response code {$r}\n";
}
function subscribe() {
	echo "Subscribed to a topic\n";
}
function message($message) {
	printf("Got a message on topic %s with payload:\n%s\n", $message->topic, $message->payload);
}
function disconnect() {
	echo "Disconnected cleanly\n";
}

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /