Skip to content

Commit

Permalink
Support mqtt over websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Jan 17, 2025
1 parent 5cc7ef3 commit 24f8d0e
Show file tree
Hide file tree
Showing 23 changed files with 1,358 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- name: Setup Dependencies
env:
COMPOSER_ROOT_VERSION: 1.x-dev
COMPOSER_ROOT_VERSION: 2.x-dev
run:
composer install -o

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Setup Dependencies
env:
COMPOSER_ROOT_VERSION: 1.x-dev
COMPOSER_ROOT_VERSION: 2.x-dev
run: composer install -o

- name: Coding Standards Check
Expand Down
2 changes: 2 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

支持 MQTT 协议 `3.1``3.1.1``5.0` 版本,支持`QoS 0``QoS 1``QoS 2`

支持 MQTT over WebSocket。

> 首个支持 MQTT `5.0` 协议的 PHP library。
[![License](https://poser.pugx.org/simps/mqtt/license)](LICENSE)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Support for MQTT protocol versions `3.1`, `3.1.1` and `5.0`.

Support for `QoS 0`, `QoS 1`, `QoS 2`.

Support for MQTT over WebSocket.

> The first PHP library to support the MQTT `5.0` protocol.
[![License](https://poser.pugx.org/simps/mqtt/license)](LICENSE)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
}
}
}
}
1 change: 1 addition & 0 deletions docs/_coverpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Support for QoS 0, 1, 2
- Support for MQTT 3.1, 3.1.1, 5.0
- Support for MQTT over WebSocket
- The first PHP library to support the MQTT 5.0 protocol

[Gitee](https://gitee.com/phpmqtt/mqtt)
Expand Down
2 changes: 2 additions & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Support for MQTT protocol versions `3.1`, `3.1.1` and `5.0`.

Support for `QoS 0`, `QoS 1`, `QoS 2`.

Support for MQTT over WebSocket.

## Requirements

* PHP >= `7.1`
Expand Down
1 change: 1 addition & 0 deletions docs/en/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* MQTT Coroutine Client
* [Client API](en/client)
* [WebSocket Client API](en/websocket)

* MQTT Protocol Analysis
* [Protocol API](en/protocol)
Expand Down
32 changes: 16 additions & 16 deletions docs/en/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Create a MQTT client instance
Simps\MQTT\Client::__construct(string $host, int $port, ClientConfig $config, int $clientType = Client::COROUTINE_CLIENT_TYPE)
```

* `string $host`
- `string $host`

Broker's host

* `int $port`
- `int $port`

Broker's port

* `ClientConfig $config`
- `ClientConfig $config`

ClientConfig object.

Expand All @@ -39,9 +39,9 @@ $configObj = new Simps\MQTT\Config\ClientConfig($config);
$client = new Simps\MQTT\Client('127.0.0.1', 1883, $configObj);
```

!> The Client will use the corresponding protocol resolution according to the `protocol_level` set.
!> The Client will use the corresponding protocol resolution according to the `protocolLevel` set.

* `int $clientType`
- `int $clientType`

Set the client type, use a Coroutine Client or a Sync Client, the default is Coroutine Client.

Expand All @@ -55,13 +55,13 @@ Connect Broker
Simps\MQTT\Client->connect(bool $clean = true, array $will = [])
```

* `bool $clean`
- `bool $clean`

Clean session. default is `true`.

For a detailed description, please see the corresponding protocol document: `Clean Session`.

* `array $will`
- `array $will`

When a client is disconnected, Broker will automatically send a will message to other clients

Expand Down Expand Up @@ -91,13 +91,13 @@ Subscribe to one topic or multiple topics
Simps\MQTT\Client->subscribe(array $topics, array $properties = [])
```

* `array $topics`
- `array $topics`

```php
// MQTT 3.x
$topics = [
// topic => Qos
'topic1' => 0,
'topic1' => 0,
'topic2' => 1,
];

Expand All @@ -109,17 +109,17 @@ $topics = [
'no_local' => true,
'retain_as_published' => true,
'retain_handling' => 2,
],
],
'topic2' => [
'qos' => 2,
'no_local' => false,
'retain_as_published' => true,
'retain_handling' => 1,
],
],
];
```

* `array $properties`
- `array $properties`

Optional in MQTT5

Expand All @@ -131,13 +131,13 @@ Unsubscribe from a topic or multiple topics
Simps\MQTT\Client->unSubscribe(array $topics, array $properties = [])
```

* `array $topics`
- `array $topics`

```php
$topics = ['topic1', 'topic2'];
```

* `array $properties`
- `array $properties`

Optional in MQTT5

Expand Down Expand Up @@ -173,11 +173,11 @@ Send messages
Simps\MQTT\Client->send(array $data, $response = true)
```

* `array $data`
- `array $data`

`$data` is the data to be sent and must contain information such as `type`

* `bool $response`
- `bool $response`

Are acknowledgements required. If `true`, `recv()` is called once

Expand Down
214 changes: 214 additions & 0 deletions docs/en/websocket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# Client API

## __construct()

Create a MQTT client instance

```php
Simps\MQTT\WebSocketClient::__construct(string $host, int $port, ClientConfig $config, string $path = '/mqtt', bool $ssl = false)
```

- `string $host`

Broker's host

- `int $port`

Broker's port

- `ClientConfig $config`

ClientConfig object.

- `string $path`

WebSocket path, default is `/mqtt`

- `bool $ssl`

Whether to use SSL, default is `false`

Example.

```php
$config = [
'userName' => '',
'password' => '',
'clientId' => '',
'keepAlive' => 10,
'protocolName' => 'MQTT', // or MQIsdp
'protocolLevel' => 4, // or 3, 5
'properties' => [], // optional in MQTT5
'delay' => 3000, // 3s
'maxAttempts' => 5,
'swooleConfig' => []
];
$configObj = new Simps\MQTT\Config\ClientConfig($config);
$client = new Simps\MQTT\WebSocketClient('broker.emqx.io', 8083, $configObj, '/mqtt');
```

## connect()

Connect Broker

```php
Simps\MQTT\WebSocketClient->connect(bool $clean = true, array $will = [])
```

- `bool $clean`

Clean session. default is `true`.

For a detailed description, please see the corresponding protocol document: `Clean Session`.

- `array $will`

When a client is disconnected, Broker will automatically send a will message to other clients

```php
$will = [
'topic' => '',
'qos' => 1,
'retain' => 0,
'message' => '', // message content
'properties' => [], // optional in MQTT5
];
```

## publish()

push a message to a topic

```php
Simps\MQTT\WebSocketClient->publish($topic, $message, $qos = 0, $dup = 0, $retain = 0, array $properties = [])
```

## subscribe()

Subscribe to one topic or multiple topics

```php
Simps\MQTT\WebSocketClient->subscribe(array $topics, array $properties = [])
```

- `array $topics`

```php
// MQTT 3.x
$topics = [
// topic => Qos
'topic1' => 0,
'topic2' => 1,
];

// MQTT 5.0
$topics = [
// topic => options
'topic1' => [
'qos' => 1,
'no_local' => true,
'retain_as_published' => true,
'retain_handling' => 2,
],
'topic2' => [
'qos' => 2,
'no_local' => false,
'retain_as_published' => true,
'retain_handling' => 1,
],
];
```

- `array $properties`

Optional in MQTT5

## unSubscribe()

Unsubscribe from a topic or multiple topics

```php
Simps\MQTT\WebSocketClient->unSubscribe(array $topics, array $properties = [])
```

- `array $topics`

```php
$topics = ['topic1', 'topic2'];
```

- `array $properties`

Optional in MQTT5

## close()

Disconnect from Broker connect. The `DISCONNECT(14)` message is send to Broker

```php
Simps\MQTT\WebSocketClient->close(int $code = ReasonCode::NORMAL_DISCONNECTION, array $properties = [])
```

## auth()

New AUTH type added in MQTT5. Authentication exchange.

```php
Simps\MQTT\WebSocketClient->auth(int $code = ReasonCode::SUCCESS, array $properties = [])
```

## recv()

Receive messages

```php
Simps\MQTT\WebSocketClient->recv(): bool|arary|string
```

## send()

Send messages

```php
Simps\MQTT\WebSocketClient->send(array $data, $response = true)
```

- `array $data`

`$data` is the data to be sent and must contain information such as `type`

- `bool $response`

Are acknowledgements required. If `true`, `recv()` is called once

## ping()

Send a heartbeat

```php
Simps\MQTT\WebSocketClient->ping()
```

## buildMessageId()

Generate MessageId

```php
Simps\MQTT\WebSocketClient->buildMessageId()
```

## genClientId()

Generate ClientId

```php
Simps\MQTT\WebSocketClient::genClientID(string $prefix = 'Simps_')
```

## getClient()

Get an instance of `Swoole\Coroutine\Http\Client`

```php
Simps\MQTT\WebSocketClient->getClient()
```
Loading

0 comments on commit 24f8d0e

Please sign in to comment.