@streamr/node 100.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +78 -0
- package/README.md +54 -0
- package/bin/broker.ts +36 -0
- package/bin/config-wizard.js +4 -0
- package/bin/config-wizard.ts +17 -0
- package/bin/delete-expired-data.ts +41 -0
- package/bin/entry-point.ts +27 -0
- package/configs/development-1.env.json +68 -0
- package/configs/development-2.env.json +53 -0
- package/configs/development-3.env.json +53 -0
- package/configs/development-prod-resend.env.json +21 -0
- package/configs/docker-1.env.json +72 -0
- package/configs/docker-2.env.json +58 -0
- package/configs/docker-3.env.json +58 -0
- package/configuration.md +38 -0
- package/dist/bin/broker.d.ts +2 -0
- package/dist/bin/broker.js +42 -0
- package/dist/bin/broker.js.map +1 -0
- package/dist/bin/config-wizard.d.ts +2 -0
- package/dist/bin/config-wizard.js +22 -0
- package/dist/bin/config-wizard.js.map +1 -0
- package/dist/bin/delete-expired-data.d.ts +2 -0
- package/dist/bin/delete-expired-data.js +44 -0
- package/dist/bin/delete-expired-data.js.map +1 -0
- package/dist/bin/entry-point.d.ts +2 -0
- package/dist/bin/entry-point.js +28 -0
- package/dist/bin/entry-point.js.map +1 -0
- package/dist/package.json +78 -0
- package/dist/src/Plugin.d.ts +33 -0
- package/dist/src/Plugin.js +43 -0
- package/dist/src/Plugin.js.map +1 -0
- package/dist/src/apiAuthentication.d.ts +4 -0
- package/dist/src/apiAuthentication.js +16 -0
- package/dist/src/apiAuthentication.js.map +1 -0
- package/dist/src/broker.d.ts +8 -0
- package/dist/src/broker.js +69 -0
- package/dist/src/broker.js.map +1 -0
- package/dist/src/config/ConfigWizard.d.ts +5 -0
- package/dist/src/config/ConfigWizard.js +466 -0
- package/dist/src/config/ConfigWizard.js.map +1 -0
- package/dist/src/config/config.d.ts +26 -0
- package/dist/src/config/config.js +92 -0
- package/dist/src/config/config.js.map +1 -0
- package/dist/src/config/config.schema.json +86 -0
- package/dist/src/config/definitions.schema.json +35 -0
- package/dist/src/config/migration.d.ts +6 -0
- package/dist/src/config/migration.js +210 -0
- package/dist/src/config/migration.js.map +1 -0
- package/dist/src/config/validateConfig.d.ts +4 -0
- package/dist/src/config/validateConfig.js +40 -0
- package/dist/src/config/validateConfig.js.map +1 -0
- package/dist/src/exports.d.ts +3 -0
- package/dist/src/exports.js +6 -0
- package/dist/src/exports.js.map +1 -0
- package/dist/src/helpers/PayloadFormat.d.ts +19 -0
- package/dist/src/helpers/PayloadFormat.js +85 -0
- package/dist/src/helpers/PayloadFormat.js.map +1 -0
- package/dist/src/helpers/applyPluginClientConfigs.d.ts +3 -0
- package/dist/src/helpers/applyPluginClientConfigs.js +29 -0
- package/dist/src/helpers/applyPluginClientConfigs.js.map +1 -0
- package/dist/src/helpers/fetchOrThrow.d.ts +2 -0
- package/dist/src/helpers/fetchOrThrow.js +20 -0
- package/dist/src/helpers/fetchOrThrow.js.map +1 -0
- package/dist/src/helpers/generateMnemonicFromAddress.d.ts +5 -0
- package/dist/src/helpers/generateMnemonicFromAddress.js +16 -0
- package/dist/src/helpers/generateMnemonicFromAddress.js.map +1 -0
- package/dist/src/helpers/multiply.d.ts +1 -0
- package/dist/src/helpers/multiply.js +10 -0
- package/dist/src/helpers/multiply.js.map +1 -0
- package/dist/src/helpers/parser.d.ts +9 -0
- package/dist/src/helpers/parser.js +62 -0
- package/dist/src/helpers/parser.js.map +1 -0
- package/dist/src/helpers/partitions.d.ts +8 -0
- package/dist/src/helpers/partitions.js +32 -0
- package/dist/src/helpers/partitions.js.map +1 -0
- package/dist/src/helpers/weightedSample.d.ts +16 -0
- package/dist/src/helpers/weightedSample.js +35 -0
- package/dist/src/helpers/weightedSample.js.map +1 -0
- package/dist/src/httpServer.d.ts +16 -0
- package/dist/src/httpServer.js +71 -0
- package/dist/src/httpServer.js.map +1 -0
- package/dist/src/pluginRegistry.d.ts +3 -0
- package/dist/src/pluginRegistry.js +35 -0
- package/dist/src/pluginRegistry.js.map +1 -0
- package/dist/src/plugins/consoleMetrics/ConsoleMetricsPlugin.d.ts +12 -0
- package/dist/src/plugins/consoleMetrics/ConsoleMetricsPlugin.js +36 -0
- package/dist/src/plugins/consoleMetrics/ConsoleMetricsPlugin.js.map +1 -0
- package/dist/src/plugins/consoleMetrics/config.schema.json +18 -0
- package/dist/src/plugins/http/HttpPlugin.d.ts +8 -0
- package/dist/src/plugins/http/HttpPlugin.js +23 -0
- package/dist/src/plugins/http/HttpPlugin.js.map +1 -0
- package/dist/src/plugins/http/config.schema.json +12 -0
- package/dist/src/plugins/http/publishEndpoint.d.ts +3 -0
- package/dist/src/plugins/http/publishEndpoint.js +72 -0
- package/dist/src/plugins/http/publishEndpoint.js.map +1 -0
- package/dist/src/plugins/info/InfoPlugin.d.ts +9 -0
- package/dist/src/plugins/info/InfoPlugin.js +31 -0
- package/dist/src/plugins/info/InfoPlugin.js.map +1 -0
- package/dist/src/plugins/info/config.schema.json +12 -0
- package/dist/src/plugins/mqtt/Bridge.d.ts +40 -0
- package/dist/src/plugins/mqtt/Bridge.js +136 -0
- package/dist/src/plugins/mqtt/Bridge.js.map +1 -0
- package/dist/src/plugins/mqtt/MqttPlugin.d.ts +14 -0
- package/dist/src/plugins/mqtt/MqttPlugin.js +30 -0
- package/dist/src/plugins/mqtt/MqttPlugin.js.map +1 -0
- package/dist/src/plugins/mqtt/MqttServer.d.ts +22 -0
- package/dist/src/plugins/mqtt/MqttServer.js +109 -0
- package/dist/src/plugins/mqtt/MqttServer.js.map +1 -0
- package/dist/src/plugins/mqtt/config.schema.json +26 -0
- package/dist/src/plugins/operator/ConsistentHashRing.d.ts +20 -0
- package/dist/src/plugins/operator/ConsistentHashRing.js +64 -0
- package/dist/src/plugins/operator/ConsistentHashRing.js.map +1 -0
- package/dist/src/plugins/operator/ContractFacade.d.ts +80 -0
- package/dist/src/plugins/operator/ContractFacade.js +364 -0
- package/dist/src/plugins/operator/ContractFacade.js.map +1 -0
- package/dist/src/plugins/operator/MaintainTopologyHelper.d.ts +23 -0
- package/dist/src/plugins/operator/MaintainTopologyHelper.js +75 -0
- package/dist/src/plugins/operator/MaintainTopologyHelper.js.map +1 -0
- package/dist/src/plugins/operator/MaintainTopologyService.d.ts +11 -0
- package/dist/src/plugins/operator/MaintainTopologyService.js +57 -0
- package/dist/src/plugins/operator/MaintainTopologyService.js.map +1 -0
- package/dist/src/plugins/operator/OperatorFleetState.d.ts +33 -0
- package/dist/src/plugins/operator/OperatorFleetState.js +112 -0
- package/dist/src/plugins/operator/OperatorFleetState.js.map +1 -0
- package/dist/src/plugins/operator/OperatorPlugin.d.ts +50 -0
- package/dist/src/plugins/operator/OperatorPlugin.js +159 -0
- package/dist/src/plugins/operator/OperatorPlugin.js.map +1 -0
- package/dist/src/plugins/operator/StreamPartAssignments.d.ts +28 -0
- package/dist/src/plugins/operator/StreamPartAssignments.js +104 -0
- package/dist/src/plugins/operator/StreamPartAssignments.js.map +1 -0
- package/dist/src/plugins/operator/announceNodeToContract.d.ts +3 -0
- package/dist/src/plugins/operator/announceNodeToContract.js +39 -0
- package/dist/src/plugins/operator/announceNodeToContract.js.map +1 -0
- package/dist/src/plugins/operator/announceNodeToStream.d.ts +3 -0
- package/dist/src/plugins/operator/announceNodeToStream.js +25 -0
- package/dist/src/plugins/operator/announceNodeToStream.js.map +1 -0
- package/dist/src/plugins/operator/checkOperatorValueBreach.d.ts +2 -0
- package/dist/src/plugins/operator/checkOperatorValueBreach.js +21 -0
- package/dist/src/plugins/operator/checkOperatorValueBreach.js.map +1 -0
- package/dist/src/plugins/operator/closeExpiredFlags.d.ts +3 -0
- package/dist/src/plugins/operator/closeExpiredFlags.js +24 -0
- package/dist/src/plugins/operator/closeExpiredFlags.js.map +1 -0
- package/dist/src/plugins/operator/config.schema.json +152 -0
- package/dist/src/plugins/operator/createIsLeaderFn.d.ts +4 -0
- package/dist/src/plugins/operator/createIsLeaderFn.js +14 -0
- package/dist/src/plugins/operator/createIsLeaderFn.js.map +1 -0
- package/dist/src/plugins/operator/fetchRedundancyFactor.d.ts +2 -0
- package/dist/src/plugins/operator/fetchRedundancyFactor.js +43 -0
- package/dist/src/plugins/operator/fetchRedundancyFactor.js.map +1 -0
- package/dist/src/plugins/operator/formCoordinationStreamId.d.ts +3 -0
- package/dist/src/plugins/operator/formCoordinationStreamId.js +9 -0
- package/dist/src/plugins/operator/formCoordinationStreamId.js.map +1 -0
- package/dist/src/plugins/operator/heartbeatUtils.d.ts +67 -0
- package/dist/src/plugins/operator/heartbeatUtils.js +26 -0
- package/dist/src/plugins/operator/heartbeatUtils.js.map +1 -0
- package/dist/src/plugins/operator/inspectOverTime.d.ts +22 -0
- package/dist/src/plugins/operator/inspectOverTime.js +146 -0
- package/dist/src/plugins/operator/inspectOverTime.js.map +1 -0
- package/dist/src/plugins/operator/inspectRandomNode.d.ts +8 -0
- package/dist/src/plugins/operator/inspectRandomNode.js +44 -0
- package/dist/src/plugins/operator/inspectRandomNode.js.map +1 -0
- package/dist/src/plugins/operator/inspectionUtils.d.ts +23 -0
- package/dist/src/plugins/operator/inspectionUtils.js +120 -0
- package/dist/src/plugins/operator/inspectionUtils.js.map +1 -0
- package/dist/src/plugins/operator/maintainOperatorValue.d.ts +2 -0
- package/dist/src/plugins/operator/maintainOperatorValue.js +21 -0
- package/dist/src/plugins/operator/maintainOperatorValue.js.map +1 -0
- package/dist/src/plugins/operator/reviewSuspectNode.d.ts +24 -0
- package/dist/src/plugins/operator/reviewSuspectNode.js +56 -0
- package/dist/src/plugins/operator/reviewSuspectNode.js.map +1 -0
- package/dist/src/plugins/storage/Batch.d.ts +51 -0
- package/dist/src/plugins/storage/Batch.js +121 -0
- package/dist/src/plugins/storage/Batch.js.map +1 -0
- package/dist/src/plugins/storage/BatchManager.d.ts +27 -0
- package/dist/src/plugins/storage/BatchManager.js +117 -0
- package/dist/src/plugins/storage/BatchManager.js.map +1 -0
- package/dist/src/plugins/storage/Bucket.d.ts +23 -0
- package/dist/src/plugins/storage/Bucket.js +90 -0
- package/dist/src/plugins/storage/Bucket.js.map +1 -0
- package/dist/src/plugins/storage/BucketManager.d.ts +56 -0
- package/dist/src/plugins/storage/BucketManager.js +306 -0
- package/dist/src/plugins/storage/BucketManager.js.map +1 -0
- package/dist/src/plugins/storage/DataQueryFormat.d.ts +10 -0
- package/dist/src/plugins/storage/DataQueryFormat.js +51 -0
- package/dist/src/plugins/storage/DataQueryFormat.js.map +1 -0
- package/dist/src/plugins/storage/DeleteExpiredCmd.d.ts +28 -0
- package/dist/src/plugins/storage/DeleteExpiredCmd.js +155 -0
- package/dist/src/plugins/storage/DeleteExpiredCmd.js.map +1 -0
- package/dist/src/plugins/storage/SetMembershipSynchronizer.d.ts +33 -0
- package/dist/src/plugins/storage/SetMembershipSynchronizer.js +102 -0
- package/dist/src/plugins/storage/SetMembershipSynchronizer.js.map +1 -0
- package/dist/src/plugins/storage/Storage.d.ts +47 -0
- package/dist/src/plugins/storage/Storage.js +459 -0
- package/dist/src/plugins/storage/Storage.js.map +1 -0
- package/dist/src/plugins/storage/StorageConfig.d.ts +43 -0
- package/dist/src/plugins/storage/StorageConfig.js +83 -0
- package/dist/src/plugins/storage/StorageConfig.js.map +1 -0
- package/dist/src/plugins/storage/StorageEventListener.d.ts +17 -0
- package/dist/src/plugins/storage/StorageEventListener.js +46 -0
- package/dist/src/plugins/storage/StorageEventListener.js.map +1 -0
- package/dist/src/plugins/storage/StoragePlugin.d.ts +32 -0
- package/dist/src/plugins/storage/StoragePlugin.js +103 -0
- package/dist/src/plugins/storage/StoragePlugin.js.map +1 -0
- package/dist/src/plugins/storage/StoragePoller.d.ts +15 -0
- package/dist/src/plugins/storage/StoragePoller.js +47 -0
- package/dist/src/plugins/storage/StoragePoller.js.map +1 -0
- package/dist/src/plugins/storage/config.schema.json +88 -0
- package/dist/src/plugins/storage/dataMetadataEndpoint.d.ts +3 -0
- package/dist/src/plugins/storage/dataMetadataEndpoint.js +35 -0
- package/dist/src/plugins/storage/dataMetadataEndpoint.js.map +1 -0
- package/dist/src/plugins/storage/dataQueryEndpoint.d.ts +6 -0
- package/dist/src/plugins/storage/dataQueryEndpoint.js +181 -0
- package/dist/src/plugins/storage/dataQueryEndpoint.js.map +1 -0
- package/dist/src/plugins/storage/storageConfigEndpoint.d.ts +3 -0
- package/dist/src/plugins/storage/storageConfigEndpoint.js +31 -0
- package/dist/src/plugins/storage/storageConfigEndpoint.js.map +1 -0
- package/dist/src/plugins/subscriber/SubscriberPlugin.d.ts +16 -0
- package/dist/src/plugins/subscriber/SubscriberPlugin.js +22 -0
- package/dist/src/plugins/subscriber/SubscriberPlugin.js.map +1 -0
- package/dist/src/plugins/subscriber/config.schema.json +31 -0
- package/dist/src/plugins/websocket/Connection.d.ts +9 -0
- package/dist/src/plugins/websocket/Connection.js +45 -0
- package/dist/src/plugins/websocket/Connection.js.map +1 -0
- package/dist/src/plugins/websocket/PublishConnection.d.ts +12 -0
- package/dist/src/plugins/websocket/PublishConnection.js +46 -0
- package/dist/src/plugins/websocket/PublishConnection.js.map +1 -0
- package/dist/src/plugins/websocket/SubscribeConnection.d.ts +13 -0
- package/dist/src/plugins/websocket/SubscribeConnection.js +50 -0
- package/dist/src/plugins/websocket/SubscribeConnection.js.map +1 -0
- package/dist/src/plugins/websocket/WebsocketPlugin.d.ts +19 -0
- package/dist/src/plugins/websocket/WebsocketPlugin.js +26 -0
- package/dist/src/plugins/websocket/WebsocketPlugin.js.map +1 -0
- package/dist/src/plugins/websocket/WebsocketServer.d.ts +16 -0
- package/dist/src/plugins/websocket/WebsocketServer.js +132 -0
- package/dist/src/plugins/websocket/WebsocketServer.js.map +1 -0
- package/dist/src/plugins/websocket/config.schema.json +51 -0
- package/package.json +78 -0
- package/plugins.md +318 -0
package/plugins.md
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# Plugins
|
|
2
|
+
|
|
3
|
+
The Broker ships with a number of plugins that add functionality or APIs.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
- [Authentication](#authentication)
|
|
7
|
+
- [Ports](#ports)
|
|
8
|
+
- [Websocket](#websocket)
|
|
9
|
+
- [MQTT](#mqtt)
|
|
10
|
+
- [HTTP](#http)
|
|
11
|
+
|
|
12
|
+
## Authentication
|
|
13
|
+
|
|
14
|
+
The integration APIs exposed by plugins can be secured via API keys. In your Broker config file, define some API keys under the root level `apiAuthentication` object:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
{
|
|
18
|
+
"plugins": ...
|
|
19
|
+
"apiAuthentication": {
|
|
20
|
+
"keys": ["my-secret-api-key"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
How to pass the API key depends on the protocol in question and is described in the sections below.
|
|
26
|
+
|
|
27
|
+
### Plugin-specific authentication
|
|
28
|
+
|
|
29
|
+
Alternatively, if you need you plugin-specific API keys, you can configure similar `apiAuthentication` object inside a plugin config. In that case case the global API keys are not used for the plugin.
|
|
30
|
+
|
|
31
|
+
## Ports
|
|
32
|
+
|
|
33
|
+
The integration plugins open TCP server ports to allow applications to connect to them. The ports need to be reachable by those applications, meaning that you may need to allow the port in your firewall and potentially set up appropriate port forwarding in your router. The port number is configurable for each plugin (see below for details).
|
|
34
|
+
|
|
35
|
+
Note that the Streamr protocol itself (used for communication between nodes) does not require any ports to be opened.
|
|
36
|
+
|
|
37
|
+
## Websocket
|
|
38
|
+
|
|
39
|
+
The `websocket` plugin provides a websocket interface for publishing and subscribing.
|
|
40
|
+
|
|
41
|
+
To enable the Websocket plugin, define a `websocket` object in the `plugins` section of the Broker configuration file:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
plugins: {
|
|
45
|
+
"websocket": {}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
You can subscribe by creating a standard JavaScript Websocket connection:
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
const socket = new WebSocket(`ws://localhost:${port}/streams/${encodeURIComponent(streamId)}/subscribe`)
|
|
53
|
+
socket.addEventListener('message', (message) => {
|
|
54
|
+
console.log(JSON.parse(message.data))
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
And publish to a stream similarly:
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
const socket = new WebSocket(`ws://localhost:${port}/streams/${encodeURIComponent(streamId)}/publish`)
|
|
62
|
+
socket.addEventListener('open', () => {
|
|
63
|
+
socket.send(JSON.stringify(message))
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Passing the API key
|
|
68
|
+
|
|
69
|
+
Pass the [API key](#authentication) by adding an `apiKey` query parameter to the connection URL:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
const socket = new WebSocket(`...?apiKey=my-secret-api-key`)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Advanced usage
|
|
76
|
+
|
|
77
|
+
#### Port
|
|
78
|
+
|
|
79
|
+
The default websocket port is `7170`. You can change it by specifying a `port` value:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
plugins: {
|
|
83
|
+
"websocket": {
|
|
84
|
+
"port": 1234
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Explicit metadata
|
|
90
|
+
|
|
91
|
+
By default the payload is the plain content of a stream message.
|
|
92
|
+
|
|
93
|
+
If you want to provide some metadata for messages (e.g. explicit timestamps), set the `payloadMetadata` option to `true`:
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
plugins: {
|
|
97
|
+
"websocket": {
|
|
98
|
+
...
|
|
99
|
+
"payloadMetadata": true
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
And restructure the payload to contain `content` and `metadata` fields:
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
const content = { "foo": "bar" }
|
|
108
|
+
const payload = {
|
|
109
|
+
content,
|
|
110
|
+
metadata: {
|
|
111
|
+
timestamp: 1234567890000
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
socket.send(JSON.stringify(payload))
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The configuration option affects also the incoming messages. Those messages will be derived in the same restructured format.
|
|
118
|
+
|
|
119
|
+
#### Partitions
|
|
120
|
+
|
|
121
|
+
If you want to publish or subscribe to a specific partition of a stream, you can add query parameters to the URL:
|
|
122
|
+
|
|
123
|
+
- `/streams/:streamId/subscribe?partitions=0,2,5`: subscribes to given partition numbers
|
|
124
|
+
- `/streams/:streamId/publish?partition=2`: publishes to given partition number
|
|
125
|
+
- `/streams/:streamId/publish?partitionKey=foo`: use the given key to calculate the partition number, [see JS-client for details](https://github.com/streamr-dev/network-monorepo/blob/main/packages/client/README.md#publishing-to-partitioned-streams))
|
|
126
|
+
- `/streams/:streamId/publish?partitionKeyField=customerId`: use the given field in a JSON to choose the `paritionKey` (e.g. `{ "customerId": "foo", ... }` -> `paritionKey` is `foo`)
|
|
127
|
+
|
|
128
|
+
By default, partition `0` is selected.
|
|
129
|
+
|
|
130
|
+
#### Secure connections
|
|
131
|
+
|
|
132
|
+
To support a SSL/TLS connections, define a SSL certificate in the Broker config:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
"sslCertificate": {
|
|
136
|
+
"certFileName": "path/cert.pem",
|
|
137
|
+
"privateKeyFileName": "path/key.pem"
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
And change the connection url to use `wss://` instead of `ws://`:
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
const socket = new WebSocket(`wss://...`)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**Note**: self-signed certificates don't work well in browser environments (the connection may not open at all). In Node environment self-signed certificates can be trusted by setting the an environment variable `NODE_TLS_REJECT_UNAUTHORIZED=0`. If possible, please obtain an authorized certificate, e.g. from [Let's Encrypt](https://letsencrypt.org).
|
|
148
|
+
|
|
149
|
+
#### Ping messages
|
|
150
|
+
|
|
151
|
+
Websocket server supports standard protocol level ping and pong messages. If you want to detect broken connections at client side, you can use e.g. [this pattern](https://github.com/websockets/ws#how-to-detect-and-close-broken-connections).
|
|
152
|
+
|
|
153
|
+
In browser environment clients are not able to send pings, as there is no `ping()` method in the standard websocket API. Therefore the websocket plugin supports also application level ping-pong: if a client sends a message which has payload of `"ping"`, server responds to it with a message which has payload of `"pong"`.
|
|
154
|
+
|
|
155
|
+
The server side has a built-it detection of broken connections. It sends a ping message if a connection has been idle for a while. All standard websocket clients respond automatically to the ping by sending a pong message. If the server doesn't receive a pong message (or other traffic) within 15 seconds, it closes the connection.
|
|
156
|
+
- See also `pingSendInterval` and `disconnectTimeout` config options
|
|
157
|
+
|
|
158
|
+
## MQTT
|
|
159
|
+
|
|
160
|
+
You can publish and subscribe to a stream using [MQTT](https://mqtt.org), making the Broker appear like a traditional MQTT broker towards connected applications and devices. To enable the MQTT plugin, define an `mqtt` object in the `plugins` section of the Broker configuration file:
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
plugins: {
|
|
164
|
+
"mqtt": {}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
You can use any MQTT client to connect to the Broker. Here's an example of subscribing to a stream with the [async-mqtt](https://www.npmjs.com/package/async-mqtt) library:
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
import mqtt from 'async-mqtt'
|
|
172
|
+
const client = await mqtt.connectAsync(`mqtt://localhost:${port}`)
|
|
173
|
+
client.on('message', (topic, message) => {
|
|
174
|
+
console.log(JSON.parse(message.toString()))
|
|
175
|
+
})
|
|
176
|
+
await client.subscribe(streamId)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Publishing data with the same library:
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
import mqtt from 'async-mqtt'
|
|
183
|
+
const client = await mqtt.connectAsync(`mqtt://localhost:${port}`)
|
|
184
|
+
await client.publish(streamId, JSON.stringify(msg))
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Passing the API key
|
|
188
|
+
|
|
189
|
+
The authentication scheme of the MQTT protocol uses a username and password. When connecting to the MQTT plugin of Streamr Broker, you can provide anything you want as the username and the [API key](#authentication) as the password:
|
|
190
|
+
```
|
|
191
|
+
mqtt.connectAsync(`mqtt://localhost:${port}`, {
|
|
192
|
+
username: 'any-username',
|
|
193
|
+
password: apiKey,
|
|
194
|
+
})
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Some MQTT clients expect the username and password to be passed in the connection URL:
|
|
198
|
+
|
|
199
|
+
```
|
|
200
|
+
mqtt://any-username:my-secret-api-key@localhost:1883
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Advanced usage
|
|
204
|
+
|
|
205
|
+
#### Port
|
|
206
|
+
|
|
207
|
+
The default port is `1883`. You can change it with `port` config option.
|
|
208
|
+
|
|
209
|
+
#### Explicit metadata
|
|
210
|
+
|
|
211
|
+
Explicit metadata can be provided the same way it is provided to the `websocket` plugin ([see above](#explicit-metadata)).
|
|
212
|
+
|
|
213
|
+
#### Topic domains
|
|
214
|
+
|
|
215
|
+
By default each MQTT `topic` matches a `streamId`. If you want to simplify the client usage, you can specify a domain, which is prefixed to a topic to make a corresponding `streamId`:
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
plugins: {
|
|
219
|
+
"mqtt": {
|
|
220
|
+
...
|
|
221
|
+
"streamIdDomain": "0x1234567890123456789012345678901234567890" // or "mydomain.eth"
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
This way you can publish and subscribe to a stream by using only the path part of a `streamId`
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
await client.publish('path-part', ...) // publishes to a stream "0x1234567890123456789012345678901234567890/path-part"
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### Partition
|
|
233
|
+
|
|
234
|
+
By default the plugin publishes and subscribes to partition `0`. If you want to specify other partition, you can encode the information into the topic string, e.g.:
|
|
235
|
+
```
|
|
236
|
+
await client.publish('/foobar?partition=5', ...)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
For publishing, it is also possible select the partition using `partitionKey`/`partitionKeyField` query parameter in the topic. See [above](#partitions) how the partition is calculated in that case.
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
## HTTP
|
|
243
|
+
|
|
244
|
+
At the moment, only publishing is supported over HTTP. To subscribe, use one of the other protocol plugins as they allow a continuous streaming connection.
|
|
245
|
+
|
|
246
|
+
To publish over HTTP, enable the `http` plugin:
|
|
247
|
+
|
|
248
|
+
```
|
|
249
|
+
plugins: {
|
|
250
|
+
"http": {}
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
The plugin provides a single endpoint: `/streams/:streamId`.
|
|
255
|
+
|
|
256
|
+
Note that the `streamId` is part of the URL and may contain slashes which need to be URL-encoded (`/` becomes `%2f`).
|
|
257
|
+
|
|
258
|
+
To publish a message to a stream, send the data as a POST payload:
|
|
259
|
+
|
|
260
|
+
```
|
|
261
|
+
curl \
|
|
262
|
+
--header 'Content-Type: application/json' \
|
|
263
|
+
--data '{"foo":"bar"}' \
|
|
264
|
+
http://localhost:7171/streams/foo.eth%2fbar
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
The endpoint returns HTTP 200 status if the message was published successfully.
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
### Passing the API key
|
|
271
|
+
|
|
272
|
+
Pass the API key in the `Authorization` header, with content `bearer <key>`, for example
|
|
273
|
+
|
|
274
|
+
```
|
|
275
|
+
Authorization: bearer my-secret-api-key
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
A curl example:
|
|
279
|
+
|
|
280
|
+
```
|
|
281
|
+
curl \
|
|
282
|
+
--header 'Content-Type: application/json' \
|
|
283
|
+
--header 'Authorization: bearer my-secret-api-key' \
|
|
284
|
+
--data '{"foo":"bar"}' \
|
|
285
|
+
http://localhost:7171/streams/foo.eth%2fbar
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Advanced usage
|
|
289
|
+
|
|
290
|
+
#### Explicit metadata
|
|
291
|
+
|
|
292
|
+
The endpoint supports the following optional query parameters:
|
|
293
|
+
|
|
294
|
+
- `timestamp` can be used to set the message timestamp explicitly. The timestamp should be passed in ISO-8601 string (e.g. `2001-02-03T04:05:06Z`), or as milliseconds since epoch, e.g. `1234567890000`
|
|
295
|
+
- `partition` (explicit partition number) or `partitionKey` (a string which used to calculate the partition number, [see JS-client for details](https://github.com/streamr-dev/network-monorepo/blob/main/packages/client/README.md#publishing-to-partitioned-streams)). The default (in case neither is provided) is to select a random partition for each message.
|
|
296
|
+
|
|
297
|
+
#### Port
|
|
298
|
+
|
|
299
|
+
The default HTTP server port is `7171`. You can change it by specifying a `port` value for the root level `httpServer` option:
|
|
300
|
+
|
|
301
|
+
```
|
|
302
|
+
"network": ...
|
|
303
|
+
"plugins": ...
|
|
304
|
+
"httpServer": {
|
|
305
|
+
"port": 1234
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### Secure connections
|
|
310
|
+
|
|
311
|
+
If you provide a SSL certificate it will support use SSL/TLS connections:
|
|
312
|
+
```
|
|
313
|
+
"httpServer": {
|
|
314
|
+
...
|
|
315
|
+
"certFileName": "path/cert.pem",
|
|
316
|
+
"privateKeyFileName": "path/key.pem"
|
|
317
|
+
}
|
|
318
|
+
```
|