Skip to content

Commit

Permalink
Merge pull request #25 from jjok/client-factory
Browse files Browse the repository at this point in the history
Add client factory
  • Loading branch information
oliverlorenz authored Aug 1, 2017
2 parents 1bf0027 + 6342673 commit 44ff9ba
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 60 deletions.
60 changes: 20 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,38 @@
# PHP MQTT Client

phpMqttClient is a mqtt client library for PHP. Its based on the reactPHP socket-client and added the mqtt protocol specific functions. I hope its a better starting point that the existing php mqtt libraries.
phpMqttClient is an MQTT client library for PHP. Its based on the reactPHP socket-client and added the MQTT protocol
specific functions. I hope its a better starting point that the existing PHP MQTT libraries.

[![Build Status](https://travis-ci.org/oliverlorenz/phpMqttClient.svg?branch=master)](https://travis-ci.org/oliverlorenz/phpMqttClient)
[![Code Climate](https://codeclimate.com/github/oliverlorenz/phpMqttClient/badges/gpa.svg)](https://codeclimate.com/github/oliverlorenz/phpMqttClient)
[![Test Coverage](https://codeclimate.com/github/oliverlorenz/phpMqttClient/badges/coverage.svg)](https://codeclimate.com/github/oliverlorenz/phpMqttClient/coverage)

### Notice - (May 12th, 2015)
This is library is not stable currently. Its an early state, but I am working on it. I will add more features if I need them. If you need features: please give feedback or contribute to get this library running.

Currently works:
* connect (clean session, no other connect flags)
* disconnect
* publish
* subscribe

## Goal

Goal of this project is easy to use MQTT client for PHP in a modern architecture without using any php modules. Currently, only protocol version 4 (mqtt 3.1.1) is implemented.
Goal of this project is easy to use MQTT client for PHP in a modern architecture without using any php modules.
Currently, only protocol version 4 (mqtt 3.1.1) is implemented.
* Protocol specifications: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html

## Example publish

```php
<?php

require __DIR__ . '/../vendor/autoload.php';

$config = include('config.php');
$config = require 'config.php';

$loop = React\EventLoop\Factory::create();

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
$connector = ClientFactory::createClient(new Version4());

$p = $connector->create($config['server'], $config['port'], $config['options']);
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
$p->then(function(Stream $stream) use ($connector) {
return $connector->publish($stream, 'a/b', 'example message');
});
$loop->run();
$connector->getLoop()->run();
```

## Example subscribe

```php
<?php
$config = require 'config.php';

use oliverlorenz\reactphpmqtt\packet\Publish;

require __DIR__ . '/../vendor/autoload.php';

$config = include('config.php');

$loop = React\EventLoop\Factory::create();

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
$connector = ClientFactory::createClient(new Version4());

$p = $connector->create($config['server'], $config['port'], $config['options']);
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
Expand All @@ -73,9 +44,18 @@ $p->then(function(\React\Stream\Stream $stream) use ($connector) {
$connector->subscribe($stream, 'a/c', 0);
});

$loop->run();
$connector->getLoop()->run();
```

## Notice - (May 12th, 2015)
This is library is not stable currently. Its an early state, but I am working on it. I will add more features if I need them. If you need features: please give feedback or contribute to get this library running.

Currently works:
* connect (clean session, no other connect flags)
* disconnect
* publish
* subscribe

## Run tests

./vendor/bin/phpunit -c ./tests/phpunit.xml ./tests
Expand Down
18 changes: 8 additions & 10 deletions examples/connectPublish.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$config = include('config.php');
use oliverlorenz\reactphpmqtt\ClientFactory;
use oliverlorenz\reactphpmqtt\protocol\Version4;
use React\Stream\Stream;

$loop = React\EventLoop\Factory::create();
require __DIR__ . '/../vendor/autoload.php';

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$config = require 'config.php';

$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
$connector = ClientFactory::createClient(new Version4(), '8.8.8.8');

$p = $connector->create($config['server'], $config['port'], $config['options']);
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
$p->then(function(Stream $stream) use ($connector) {
return $connector->publish($stream, 'hello/world', 'example message');
});

$loop->run();
$connector->getLoop()->run();
17 changes: 7 additions & 10 deletions examples/connectSubscribe.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
<?php

use oliverlorenz\reactphpmqtt\ClientFactory;
use oliverlorenz\reactphpmqtt\packet\Publish;
use oliverlorenz\reactphpmqtt\protocol\Version4;
use React\Stream\Stream;

require __DIR__ . '/../vendor/autoload.php';

$config = include('config.php');
$config = require 'config.php';

$loop = React\EventLoop\Factory::create();

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$version = new oliverlorenz\reactphpmqtt\protocol\Version4();
$connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version);
$connector = ClientFactory::createClient(new Version4(), '8.8.8.8');

$p = $connector->create($config['server'], $config['port'], $config['options']);
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
$p->then(function(Stream $stream) use ($connector) {
$stream->on(Publish::EVENT, function(Publish $message) {
printf(
'Received payload "%s" for topic "%s"%s',
Expand All @@ -28,4 +25,4 @@
return $connector->subscribe($stream, 'hello/world', 0);
});

$loop->run();
$connector->getLoop()->run();
33 changes: 33 additions & 0 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace oliverlorenz\reactphpmqtt;

use oliverlorenz\reactphpmqtt\protocol\Version;
use React\Dns\Resolver\Factory as DnsResolverFactory;
use React\EventLoop\Factory as EventLoopFactory;

class ClientFactory
{
public static function createClient(Version $version, $resolverIp = '8.8.8.8')
{
$loop = EventLoopFactory::create();
$resolver = self::createDnsResolver($resolverIp, $loop);

return new Connector($loop, $resolver, $version);
}

public static function createSecureClient(Version $version, $resolverIp = '8.8.8.8')
{
$loop = EventLoopFactory::create();
$resolver = self::createDnsResolver($resolverIp, $loop);

return new SecureConnector($loop, $resolver, $version);
}

private static function createDnsResolver($resolverIp, $loop)
{
$dnsResolverFactory = new DnsResolverFactory();

return $dnsResolverFactory->createCached($resolverIp, $loop);
}
}
26 changes: 26 additions & 0 deletions tests/unit/ClientFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace oliverlorenz\reactphpmqtt;

use oliverlorenz\reactphpmqtt\protocol\Version4;
use PHPUnit_Framework_TestCase as TestCase;

/**
* @covers \oliverlorenz\reactphpmqtt\ClientFactory
*/
final class ClientFactoryTest extends TestCase
{
public function testConnectorCanBeCreated()
{
$client = ClientFactory::createClient(new Version4(), '8.8.8.8');

$this->assertInstanceOf('oliverlorenz\reactphpmqtt\Connector', $client);
}

public function testSecureConnectorCanBeCreated()
{
$client = ClientFactory::createSecureClient(new Version4(), '8.8.8.8');

$this->assertInstanceOf('oliverlorenz\reactphpmqtt\SecureConnector', $client);
}
}

0 comments on commit 44ff9ba

Please sign in to comment.