diff --git a/src/protocols/1/essentials/getting-started/index.md b/src/protocols/1/essentials/getting-started/index.md index e2346340b..1cd4fea58 100644 --- a/src/protocols/1/essentials/getting-started/index.md +++ b/src/protocols/1/essentials/getting-started/index.md @@ -6,7 +6,7 @@ order: 0 # Getting Started -Kuzzle has native support for the following network protocols: HTTP, Websocket and Socket.io. +Kuzzle has native support for the following network protocols: [HTTP]({{ site_base_path }}protocols/1/native-protocols/http), [MQTT]({{ site_base_path }}protocols/1/native-protocols/mqtt) (disabled by default), [Websocket]({{ site_base_path }}protocols/1/native-protocols/websocket) and [Socket.io]({{ site_base_path }}protocols/1/native-protocols/socketio). However, any number of protocols can be implemented, adding new network capabilities. @@ -18,8 +18,6 @@ Protocols are provided with objects to interact with Kuzzle: * [EntryPoint]({{ site_base_path }}protocols/1/entrypoint): base communication layer (declare user connections, forward API requests, ...) * [context]({{ site_base_path }}protocols/1/context): utilities and object constructors not directly related to network communications -Protocol implementation example: [MQTT](https://github.com/kuzzleio/protocol-mqtt) - --- ## Prerequisites diff --git a/src/protocols/1/essentials/broadcast/index.md b/src/protocols/1/methods/broadcast/index.md similarity index 100% rename from src/protocols/1/essentials/broadcast/index.md rename to src/protocols/1/methods/broadcast/index.md diff --git a/src/protocols/1/essentials/disconnect/index.md b/src/protocols/1/methods/disconnect/index.md similarity index 100% rename from src/protocols/1/essentials/disconnect/index.md rename to src/protocols/1/methods/disconnect/index.md diff --git a/src/protocols/1/methods/index.md b/src/protocols/1/methods/index.md new file mode 100644 index 000000000..36deb02c4 --- /dev/null +++ b/src/protocols/1/methods/index.md @@ -0,0 +1,6 @@ +--- +layout: full.html.hbs +title: Protocol Methods +description: Extend Kuzzle communication capabilities +order: 0 +--- diff --git a/src/protocols/1/essentials/init/index.md b/src/protocols/1/methods/init/index.md similarity index 100% rename from src/protocols/1/essentials/init/index.md rename to src/protocols/1/methods/init/index.md diff --git a/src/protocols/1/essentials/joinchannel/index.md b/src/protocols/1/methods/joinchannel/index.md similarity index 100% rename from src/protocols/1/essentials/joinchannel/index.md rename to src/protocols/1/methods/joinchannel/index.md diff --git a/src/protocols/1/essentials/leavechannel/index.md b/src/protocols/1/methods/leavechannel/index.md similarity index 100% rename from src/protocols/1/essentials/leavechannel/index.md rename to src/protocols/1/methods/leavechannel/index.md diff --git a/src/protocols/1/essentials/notify/index.md b/src/protocols/1/methods/notify/index.md similarity index 100% rename from src/protocols/1/essentials/notify/index.md rename to src/protocols/1/methods/notify/index.md diff --git a/src/protocols/1/native-protocols/http/index.md b/src/protocols/1/native-protocols/http/index.md new file mode 100644 index 000000000..4fa692fa6 --- /dev/null +++ b/src/protocols/1/native-protocols/http/index.md @@ -0,0 +1,32 @@ +--- +layout: full.html.hbs +title: HTTP +order: 0 +--- + +# HTTP + +## Configuration + +The protocol can be configured via the [kuzzlerc configuration file]({{ site_base_path }}guide/1/essentials/configuration/), under the ``server > protocols > http`` section. + +| Option | Type | Description | Default | +|---|---|---|---| +| ``enabled`` |
boolean| Enable/Disable HTTP protocol support | ``true`` | +| ``maxFormFileSize`` |
string| Maximum size of requests sent via http forms | ``1mb`` | +| ``maxEncodingLayers`` |
integer| Maximum number of encoding layers that can be applied to an http message, using the Content-Encoding header. This parameter is meant to prevent abuses by setting an abnormally large number of encodings, forcing Kuzzle to allocate as many decoders to handle the incoming request | ``3`` | +| ``allowCompression`` |
boolean| Enable support for compressed requests, using the Content-encoding header. Currently supported compression algorithms: gzip, deflate, identity. Note: "identity" is always an accepted value, even if compression support is disabled | ``true`` | + +### Configure listening port + +
boolean| Enable/Disable the MQTT protocol support | ``false`` | +| ``allowPubSub`` |
boolean| Allow MQTT pub/sub capabilities or restrict to Kuzzle requests only | ``false`` | +| ``developmentMode`` |
boolean| Switches ``responseTopic`` back to a regular public topic | ``false`` | +| ``disconnectDelay`` |
integer| Delay in ms to apply between a disconnection notification is received and the connection is actually removed | 250 | +| ``requestTopic`` |
string| Name of the topic listened by the plugin for requests | ``"Kuzzle/request"`` | +| ``responseTopic`` |
string| Name of the topic clients should listen to get requests result | ``"Kuzzle/response"`` | +| ``server`` |
object| Constructor options pased to underlying mqtt server. See [mosca documentation](https://github.com/mcollina/mosca/wiki/Mosca-advanced-usage#other-options-of-mosca-the-ones-we-inserted-in-our-moscasettings-var) for further reference. | ``{port: 1883}`` | + +example: + +``.kuzzlerc`` +```json +{ + "server": { + "protocols": { + "mqtt": { + "allowPubSub": true + } + } + } +} +``` + +## How to use + +### Sending an API request and getting the response + +By default, the MQTT protocol listens to the ``Kuzzle/request`` MQTT topic (see [configuration](#configuration)) for requests to the [Kuzzle API]({{ site_base_path }}api/1/essentials/connecting-to-kuzzle/). + +It then forwards Kuzzle response to the ``Kuzzle/response`` MQTT topic, **and only to the client who made the initial request**. + +The order of responses is not guaranteed to be the same than the order of requests. To link a response to its original request, use the ``requestId`` attribute: the response will have the same ``requestId`` than the one provided in the request. + +Example using [MQTT Node module](https://www.npmjs.com/package/mqtt): _to use a CLI client, you will need to enable development mode. Please refer to [the dedicated section below](#development-mode) for instruction and examples_ + +```javascript +const + mqtt = require('mqtt'), + client = mqtt.connect({host: 'localhost'}); + +// Sending a volatile message +client.publish('Kuzzle/request', JSON.stringify({ + index: 'index', + collection: 'collection', + controller: 'realtime', + action: 'publish', + requestId: 'some unique ID', + body: { some: "message" } +})); + +// Getting Kuzzle's response +client.on('message', (topic, raw) => { + const message = JSON.parse(Buffer.from(raw)); + + // API results topic + if (topic === 'Kuzzle/response') { + // Response to our "publish" request + if (message.requestId === 'some unique ID') { + console.log('Message publication result: ', message); + } + } +}); +``` + +### Using Kuzzle subscriptions + +Kuzzle allows to [subscribe](https://docs.kuzzle.io/api/1/controller-realtime/subscribe/) to messages and events using advanced filters. + +Each time a subscription is sent, a dedicated MQTT topic is created, named after the ``channel`` property issued by Kuzzle. + +Here are the steps to perform a Kuzzle subscription using MQTT: + +- Send a subscription request to Kuzzle +- Listen to the request's response to ge the ``channel`` identifier +- Subscribe to the MQTT topic named after this channel identifier + +Example using [MQTT Node module](https://www.npmjs.com/package/mqtt): + +```javascript +const + mqtt = require('mqtt'), + client = mqtt.connect({host: 'localhost'}), + channels = []; + +// Sending a volatile message +client.publish('Kuzzle/request', JSON.stringify({ + index: 'index', + collection: 'collection', + controller: 'realtime', + action: 'subscribe', + requestId: 'some unique ID', + body: { + term: { + some: 'filter' + } + } +})); + +// Getting Kuzzle's response +client.on('message', (topic, raw) => { + const message = JSON.parse(Buffer.from(raw)); + + // API results topic + if (topic === 'Kuzzle/response') { + // Response to our "publish" request + if (message.requestId === 'some unique ID') { + channels.push(message.result.channel); + client.subscribe(message.result.channel); + } + } + else if (channels.indexOf(topic) !== -1) { + // Subscription notification + console.log('Notification: ', message); + } +}); +``` + +## Authorizations + +### Publishing + +If ``allowPubSub`` is set to ``false``, clients can only publish to the ``requestTopic`` topic (defaults to ``Kuzzle/request``). + +If ``allowPubSub`` is set to ``true``, clients are only forbidden to publish to the ``responseTopic`` topic (defaults to ``Kuzzle/response``). + +
boolean| Enable/Disable Socket.IO protocol support | ``true`` | +| ``origins`` |
string| Value of Access-Control-Allow-Origin header to answer the upgrade request | ``*:*`` | + +### Configure listening port + +
boolean| Enable/Disable WebSocket protocol support | ``true`` | +| ``heartbeat`` |
integer| The time, in milliseconds, between the server's PING requests. Setting this value to ``0`` disables PING/PONG requests entirely. | ``60000`` | + +### Configure listening port + +