@device-portal/react 0.0.1 → 0.0.4

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.
Files changed (2) hide show
  1. package/README.md +96 -5
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -14,16 +14,22 @@ npm install @device-portal/react
14
14
 
15
15
  It is expected that the package will be used on two different devices. Create for them two separate pages or apps. Let's call them App A and App B. Both apps will be linked by same `room` (e.g. `'my-test-room'`).
16
16
 
17
- ### App A
17
+ ### One-way Data Flow
18
18
 
19
- The first app will be a value provider or `Provider`.
19
+ This is the simplest use case where a `Provider` sends data to one or more `Consumer`s.
20
+
21
+ #### App A (Provider)
22
+
23
+ The provider app sends a value.
20
24
 
21
25
  ```jsx
22
26
  import { useDevicePortalProvider } from '@device-portal/react'
27
+ import { useState } from 'react'
23
28
 
24
29
  const AppA = () => {
25
30
  const [value, setValue] = useState(0)
26
- useDevicePortalProvider('my-test-room', value.toString(), {
31
+ useDevicePortalProvider('my-test-room', {
32
+ value: value.toString(),
27
33
  websocketSignalingServer: 'wss://device-portal.filipchalupa.cz',
28
34
  })
29
35
 
@@ -43,14 +49,23 @@ const AppA = () => {
43
49
  }
44
50
  ```
45
51
 
46
- ### App B
52
+ #### App B (Consumer)
47
53
 
48
- The other app will be a value consumer or `Consumer`. Every time provider value in App A changes, the consumer in App B will be automatically updated.
54
+ The consumer app receives the value from the provider. Every time the provider's value changes, the consumer will be automatically updated.
49
55
 
50
56
  ```jsx
51
57
  import { useDevicePortalConsumer } from '@device-portal/react'
58
+ import { Suspense } from 'react'
52
59
 
53
60
  const AppB = () => {
61
+ return (
62
+ <Suspense fallback={<p>Connecting...</p>}>
63
+ <ConsumerComponent />
64
+ </Suspense>
65
+ )
66
+ }
67
+
68
+ const ConsumerComponent = () => {
54
69
  const { value } = useDevicePortalConsumer('my-test-room', {
55
70
  websocketSignalingServer: 'wss://device-portal.filipchalupa.cz',
56
71
  })
@@ -58,12 +73,88 @@ const AppB = () => {
58
73
  return (
59
74
  <>
60
75
  <h1>App B</h1>
76
+ <p>Value from provider: {value}</p>
77
+ </>
78
+ )
79
+ }
80
+ ```
81
+
82
+ ### Two-way Communication
83
+
84
+ You can also send messages from the `Consumer` back to the `Provider`.
85
+
86
+ #### App A (Provider with message handling)
87
+
88
+ The provider now also listens for messages from the consumer.
89
+
90
+ ```jsx
91
+ import { useDevicePortalProvider } from '@device-portal/react'
92
+ import { useState } from 'react'
93
+
94
+ const AppA = () => {
95
+ const [value, setValue] = useState(0)
96
+ const [messageFromB, setMessageFromB] = useState('')
97
+
98
+ useDevicePortalProvider('my-test-room', {
99
+ value: value.toString(),
100
+ onMessageFromConsumer: (message) => {
101
+ setMessageFromB(message)
102
+ },
103
+ })
104
+
105
+ return (
106
+ <>
107
+ <h1>App A</h1>
61
108
  <p>Value: {value}</p>
109
+ <button
110
+ onClick={() => {
111
+ setValue(value + 1)
112
+ }}
113
+ >
114
+ Increment
115
+ </button>
116
+ <p>Last message from App B: {messageFromB}</p>
62
117
  </>
63
118
  )
64
119
  }
65
120
  ```
66
121
 
122
+ #### App B (Consumer with message sending)
123
+
124
+ The consumer can now send messages to the provider.
125
+
126
+ ```jsx
127
+ import { useDevicePortalConsumer } from '@device-portal/react'
128
+ import { Suspense } from 'react'
129
+
130
+ const AppB = () => {
131
+ return (
132
+ <Suspense fallback={<p>Connecting…</p>}>
133
+ <ConsumerComponent />
134
+ </Suspense>
135
+ )
136
+ }
137
+
138
+ const ConsumerComponent = () => {
139
+ const { value, sendMessageToProvider } =
140
+ useDevicePortalConsumer('my-test-room')
141
+
142
+ return (
143
+ <>
144
+ <h1>App B</h1>
145
+ <p>Value from provider: {value}</p>
146
+ <button onClick={() => sendMessageToProvider('Hello from B!')}>
147
+ Send Message to A
148
+ </button>
149
+ </>
150
+ )
151
+ }
152
+ ```
153
+
154
+ ## Resilience
155
+
156
+ The WebRTC connection is designed to be resilient. If the connection to the signaling server is temporarily lost, any established peer-to-peer connections will remain active. The client will attempt to reconnect to the signaling server in the background to handle any future connection negotiations.
157
+
67
158
  ## Development
68
159
 
69
160
  Run
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@device-portal/react",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "Simple WebRTC data channel for React.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "https://github.com/FilipChalupa/react-device-portal.git"
19
+ "url": "https://github.com/FilipChalupa/device-portal.git"
20
20
  },
21
21
  "keywords": [
22
22
  "react",