Skip to content

Commit bb37155

Browse files
committed
docs: usage example for native messages
1 parent 6bba304 commit bb37155

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# @podium/browser
22

3-
This is a client-side library designed to send and receive messages between different podlets in a layout.
3+
This is a client-side library designed to:
4+
5+
- send and receive messages between different podlets in a layout.
6+
- send and receive messages between web and native in a webview.
47

58
## Usage
69

@@ -40,6 +43,36 @@ messageBus.subscribe('search', 'query', (event) => {
4043
});
4144
```
4245

46+
### Send messages between web and native
47+
48+
To send messages between web and native the [`@podium/bridge`](https://github.com/podium-lib/bridge) must be in the document. Typically you include this once in your layout so podlets can assume it's present.
49+
50+
The API is similar as sending messages between podlets. This way you can notify both other podlets and any native code using the same API.
51+
52+
```js
53+
import { MessageBus } from '@podium/browser';
54+
55+
const messageBus = new MessageBus();
56+
57+
// notify of a logout
58+
messageBus.publish('system', 'authentication', null);
59+
```
60+
61+
The `channel` and `topic` parameters are combined to form the JSON RPC 2.0 `"method"` property. In the example above the channel `system` and topic `authentication` are combined to the method `"system/authentication"`.
62+
63+
To listen to messages coming in from native:
64+
65+
```js
66+
import { MessageBus } from '@podium/browser';
67+
68+
const messageBus = new MessageBus();
69+
70+
// listen to the `"system/authentication"` message coming from native
71+
messageBus.subscribe('system', 'authentication', (event) => {
72+
console.log(event.payload);
73+
});
74+
```
75+
4376
## API
4477

4578
### MessageBus

0 commit comments

Comments
 (0)