Skip to content

Commit

Permalink
* (Apollon77) Allow sending of messages to scripts also from adapters…
Browse files Browse the repository at this point in the history
… and CLI by sending "toScript" message (see [onMessage Documentation](https://github.com/ioBroker/ioBroker.javascript/blob/master/docs/en/javascript.md#onmessage))
  • Loading branch information
Apollon77 committed May 6, 2022
1 parent becccb5 commit f1c8585
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ And then call `npm run build`.
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### __WORK IN PROGRESS__
* (Apollon77) Allow sending of messages to scripts also from adapters and CLI by sending "toScript" message (see [onMessage Documentation](https://github.com/ioBroker/ioBroker.javascript/blob/master/docs/en/javascript.md#onmessage))
* (Apollon77) Lists returned by $-selector are now unified and do not contain double entries
* (Apollon77) Fix subscribe multiple object ID lists in blockly

### 5.6.1 (2022-05-03)
* (Apollon77) Allow to subscribe multiple object ID lists in blockly
* (Apollon77) Make sure lists returned by $-selector do not contain duplicates
Expand Down
22 changes: 21 additions & 1 deletion docs/en/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -1469,9 +1469,29 @@ messageTo('messageName', dataWithNoResponse);
onMessage('messageName', (data, callback) => {console.log('Received data: ' + data); callback(null, Date.now())});
```
Subscribes on message bus and delivers response via callback.
Subscribes on javascript adapter message bus and delivers response via callback.
The response from script which sends response as first will be accepted as answer, all other answers will be ignored.
To send a message to an JavaScript script which is then received by this handler use [messageTo](#messageTo).
To send a message from any other adapter use
```
adapter.sendTo('javascript.0', 'toScript', {
script: 'script.js.messagetest',
message: 'messageName',
data: {
flag: true
}
});
```
to send a message from CLI use
```
iob message javascript.0 toScript '{"script": "script.js.messagetest", "message": "messageName", "data": {"flag": true}}'
```
### onMessageUnregister
```
const id = onMessage('messageName', (data, callback) => {console.log(data); callback(Date.now())});
Expand Down
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,13 @@ function startAdapter(options) {
if (obj) {
switch (obj.command) {
// process messageTo commands
case 'toScript':
case 'jsMessageBus':
if (obj.message && (
obj.message.instance === null ||
obj.message.instance === undefined ||
('javascript.' + obj.instance === adapter.namespace) ||
(obj.instance === adapter.namespace)
('javascript.' + obj.message.instance === adapter.namespace) ||
(obj.message.instance === adapter.namespace)
)) {
Object.keys(context.messageBusHandlers).forEach(name => {
// script name could be script.js.xxx or only xxx
Expand Down

0 comments on commit f1c8585

Please sign in to comment.