objtalk 0.3.0

a lightweight realtime database for IoT projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# objtalk

[![Documentation](https://docs.rs/objtalk/badge.svg)](https://docs.rs/objtalk)
[![Automated Builds](https://github.com/objtalk/objtalk/actions/workflows/build.yaml/badge.svg)](https://github.com/objtalk/objtalk/actions/workflows/build.yaml)

objtalk is a lightweight realtime database that can be used for IoT projects. It can be used to store and query the state of devices in realtime and also to communicate between devices using events and rpc. Think mqtt but on steroids.

objtalk stores objects. Each object has a name, a value (json) and a timestamp that indicates when the object was last modified. Objects can be created, modified, removed and queried over an API. Queries can also watch for changes and return them in real time to the client.

The API can be accessed using multiple transports. The protocol is JSON-RPC-like and can be used over TCP or over a WebSocket. HTTP/REST can also be used for simple commands.

All objects stored in objtalk can also be viewed in a browser using the builtin admin panel.

## Installation

Install a [precompiled binary](https://github.com/objtalk/objtalk/releases) or use cargo:

```
$ cargo install objtalk
```

## Using the server

Create a config file, for example called `objtalk.toml`:

```toml
[storage]
backend = "sqlite"
sqlite.filename = "objtalk.db"

[[http]]
addr = "127.0.0.1:3000"
admin.enabled = true
#admin.asset-overrides = "admin"
#allow-origin = "*"

[[tcp]]
addr = "127.0.0.1:3001"
```

Start the server:

```sh
$ objtalk-server --config objtalk.toml
http transport listening on http://127.0.0.1:3000
tcp transport listening on 127.0.0.1:3001
```

Visit the admin panel at `http://127.0.0.1:3000`.

## Using the client

```sh
$ objtalk-cli -u http://127.0.0.1:3000 set foo 42
$ objtalk-cli -u http://127.0.0.1:3000 get foo
[
    {
        "name": "foo",
        "value": 42,
        "lastModified": "2021-05-07T17:53:29.066420Z"
    }
]
$ objtalk-cli -u http://127.0.0.1:3000 remove foo
```

## Using objtalk as a rust library

The objtalk crate provides the `objtalk-server` and `objtalk-cli` binaries, but you can also use it as a library to integrate objtalk into your rust project. Take a look at the [documentation](https://docs.rs/objtalk) for a list of all available methods. You can use the `server` and `client` feature flags to trim down the library.

## Libraries for other languages

- [JavaScript]https://www.npmjs.com/package/objtalk

## Other integrations

- [Node-RED]https://flows.nodered.org/node/node-red-contrib-objtalk

## API/Protocol

### Basics

#### set `name` `value`

`set` creates or replaces an object.

using objtalk-cli:

```
$ objtalk-cli set sensor '{"temperature":20}'
```

over http:

```
$ curl -X POST 127.0.0.1:3000/objects/sensor -d '{"temperature":20}'
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "set",
    "name": "sensor",
    "value": { "temperature": 20 }
}

{
    "requestId": 1,
    "result": {
        "success": true
    }
}
```

#### patch `name` `value`

`patch` creates or updates an object. If an object with the same name already exists the value is merged (non-deep).

using objtalk-cli:

```
$ objtalk-cli replace sensor '{"temperature":20}'
```

over http:

```
$ curl -X PATCH 127.0.0.1:3000/objects/sensor -d '{"temperature":20}'
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "patch",
    "name": "sensor",
    "value": {
        "temperature": 20
    }
}

{
    "requestId": 1,
    "result": {
        "success": true
    }
}
```

#### get `pattern`

`get` returns all objects where the name matches `pattern`.

Object names look like a file path and consist of multiple parts seperated by forward slashes. Each part can be either a string, a `+` or `*`. `+` matches anything until the next slash, `*` matches anything until the end of the string. Multiple sub-patterns can be combined using a comma and are logically OR-ed togeter.

Some examples:
- `*` matches all objects
- `device/lamp/+` matches `device/lamp/livingroom` and `device/lamp/bedroom`, but not `device/sensor/livingroom`
- `device/+/livingroom` matches `device/lamp/livingroom` and `device/sensor/livingroom`
- `device/*` matches `device/lamp/livingroom`, `device/lamp/bedroom` and `device/sensor/livingroom`
- `device/lamp/+,device/sensor/+` matches `device/lamp/livingroom`, `device/lamp/bedroom` and `device/sensor/livingroom`

using objtalk-cli:

```
$ objtalk-cli get '*'
```

over http:

```
$ curl '127.0.0.1:3000/query?pattern=*'
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "get",
    "pattern": "*"
}

{
    "requestId": 1,
    "result": {
        "objects": [
            {
                "name": "sensor",
                "value": { "temperature": 20 },
                "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
            }
        ]
    }
}
```

#### query `pattern`

`query` returns all objects matching `pattern` and watches for changes. The initial response contains all objects that match `pattern`. When new objects with a matching name are created, changed or removed a `queryAdd` event, `queryChange` event or `queryRemove` event is emitted.

using objtalk-cli: unsupported

over http:

```
$ curl '127.0.0.1:3000/query?pattern=*' -H "Accept: text/event-stream"
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "query",
    "pattern": "*"
}

{
    "requestId": 1,
    "result": {
        "queryId": "01234567-89ab-cdef-0123-456789abcdef",
        "objects": [
            {
                "name": "sensor",
                "value": { "temperature": 20 },
                "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
            }
        ]
    }
}

{
    "type": "queryChange",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef",
    "object": {
        "name": "sensor",
        "value": { "temperature": 21 },
        "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
    }
}

{
    "type": "queryAdd",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef",
    "object": {
        "name": "foo",
        "value": { "bar": true },
        "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
    }
}

{
    "type": "queryChange",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef",
    "object": {
        "name": "foo",
        "value": { "bar": false },
        "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
    }
}

{
    "type": "queryRemove",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef",
    "object": {
        "name": "foo",
        "value": { "bar": false },
        "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
    }
}
```

#### unsubscribe `queryId`

`unsubscribe` stops watching for changes and removes a query.

using objtalk-cli: unsupported

over http: implicit when the event-stream is closed

over tcp or websocket:

```json
{
    "id": 1,
    "type": "unsubscribe",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef"
}
{
    "requestId": 1,
    "result": {
        "success": true
    }
}
```

#### remove `name`

`remove` removes an object. The return value indicates if an object with the name existed and was successfully removed.

using objtalk-cli:

```
$ objtalk-cli remove sensor
```

over http:

```
$ curl -X DELETE 127.0.0.1:3000/objects/sensor
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "remove",
    "name": "sensor"
}

{
    "requestId": 1,
    "result": {
        "existed": true
    }
}
```

### Events

Objects can also emit events. You can listen for events by creating a query.

#### emit `object` `event` `data`

`emit` publishes an event on an existing object.

using objtalk-cli: 

```
$ objtalk-cli emit gamepad buttonPress '{"button":"a"}'
```

over http:

```
$ curl -X POST 127.0.0.1:3000/events/gamepad -d '{"event":"buttonPress","data":{"button":"a"}}'
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "emit",
    "object": "gamepad",
    "event": "buttonPress",
    "data": { "button": "a" }
}

{
    "requestId": 1,
    "result": {
        "success": true
    }
}
```

Which produces the following event on all queries that match that object:

```json
{
    "type": "queryEvent",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef",
    "object": "gamepad",
    "event": "buttonPress",
    "data": { "button": "a" }
}
```

### RPC

Methods can be called on objects. One client is connected to objtalk and listens for method calls, performs them and pushes the result to objtalk. Other clients can call these methods.

#### Calling methods

`invoke` calls a method on an object.

using objtalk-cli: 

```
$ objtalk-cli invoke device/lamp/livingroom setState '{"on":true}'
```

over http:

```
$ curl -X POST 127.0.0.1:3000/invoke/device/lamp/livingroom -d '{"method":"setState","args":{"on":true}}'
```

over tcp or websocket:

```json
{
    "id": 1,
    "type": "invoke",
    "object": "device/lamp/livingroom",
    "method": "setState",
    "args": { "on": true }
}

{
    "requestId": 1,
    "result": {
        "success": true
    }
}
```

#### Providing method calls

To provide rpc calls for an object a client ("provider") has to connect to objtalk and has to create a query with `provideRpc` set to true. Once another client ("consumer") tries to call a method on the object a `queryInvocation` event is emitted on the query. The provider can process the request and return a result to the consumer using the `invokeResult` command.

The whole flow looks like this:

```json
{
    "id": 1,
    "type": "query",
    "pattern": "device/lamp/livingroom",
    "provideRpc": true
}

{
    "requestId": 1,
    "result": {
        "queryId": "01234567-89ab-cdef-0123-456789abcdef",
        "objects": [
            {
                "name": "device/lamp/livingroom",
                "value": { "on": false },
                "lastModified": "YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ"
            }
        ]
    }
}

{
    "type": "queryInvocation",
    "queryId": "01234567-89ab-cdef-0123-456789abcdef",
    "invocationId": "fedcba98-7654-3210-fedc-ba9876543210",
    "object": "device/lamp/livingroom",
    "method": "setState",
    "args": { "on": true }
}

{
    "id": 2,
    "type": "invokeResult",
    "invocationId": "fedcba98-7654-3210-fedc-ba9876543210",
    "result": { "success": true }
}

{
    "requestId": 2,
    "result": {
        "success": true
    }
}
```

### Disconnect commands

A client can register a list of commands that are executed once the client disconnects. This can be used, for example, to indicate that a device went offline by changing the value of an object. The supported commands are `set`, `patch`, `remove` and `emit`.

#### setDisconnectCommands `commands`

`setDisconnectCommands` sets the list of commands to execute when the client disconnects.

using objtalk-cli: unsupported

over http: unsupported

over tcp or websocket:

```json
{
    "id": 1,
    "type": "setDisconnectCommands",
    "commands": [
        {
            "type": "set",
            "name": "device/foo",
            "value": { "offline": true },
        },
        {
            "type": "patch",
            "name": "device/foo",
            "value": { "offline": true },
        },
        {
            "type": "remove",
            "name": "client",
        },
        {
            "type": "emit",
            "name": "device/foo",
            "event": "offline",
            "data": {},
        }
    ]
}

{
    "requestId": 1,
    "result": {
        "success": true
    }
}
```