Open History to see the last commit
1---
2title: Bidirectional Communication
3id: bidirectional-communication
4---
5β
6Most devtools plugins observe state in one direction: app to devtools. But `EventClient` supports two-way communication. Your devtools panel can also send commands back to the app. This enables features like state editing, action replay, and time-travel debugging.
7β
8```mermaid
9graph LR
10 subgraph app["Application"]
11 state["State / Library"]
12 end
13 subgraph ec["EventClient"]
14 direction TB
15 bus["Event Bus"]
16 end
17 subgraph dt["Devtools Panel"]
18 ui["Panel UI"]
19 end
20β
21 state -- "emit('state-update')" --> bus
22 bus -- "on('state-update')" --> ui
23 ui -- "emit('reset')" --> bus
24 bus -- "on('reset')" --> state
25```
26β
27## Pattern: App to Devtools (Observation)
28β
29The standard one-way pattern. Your app emits events, the devtools panel listens.
30β
31```ts
32// In your app/library
33eventClient.emit('state-update', { count: 42 })
34β
35// In your devtools panel
36eventClient.on('state-update', (e) => {
37 setState(e.payload)
38})
39```
40β
41## Pattern: Devtools to App (Commands)
42β
43The panel emits command events, the app listens and reacts.
44β
45```ts
46// In your devtools panel (e.g., a "Reset" button click handler)
47eventClient.emit('reset', undefined)
48β
49// In your app/library
50eventClient.on('reset', () => {
51 store.reset()
52})
53```
54β
55You need to define both directions in your event map:
56β
57```ts
58type MyEvents = {
59 // App β Devtools
60 'state-update': { count: number }
61 // Devtools β App
62 'reset': void
63 'set-state': { count: number }
64}
65```
66β
67## Pattern: Time Travel
68β
69The most powerful bidirectional pattern. Combine observation with command-based state restoration.
70β
71```mermaid
72sequenceDiagram
73 participant App as Application
74 participant EC as EventClient
75 participant Panel as Time Travel Panel
76β
77 App->>EC: emit('snapshot', { state, timestamp })
78 EC->>Panel: on('snapshot') β collect snapshots
79 Note over Panel: User drags slider to past state
80 Panel->>EC: emit('revert', { state })
81 EC->>App: on('revert') β restore state
82 App->>EC: emit('snapshot', { state, timestamp })
83 EC->>Panel: on('snapshot') β update timeline
84```
85β
86### Event Map
87β
88```ts
89type TimeTravelEvents = {
90 'snapshot': { state: unknown; timestamp: number; label: string }
91 'revert': { state: unknown }
92}
93```
94β
95### App Side
96β
97Emit snapshots on every state change:
98β
99```ts
100function applyAction(action) {
101 state = reducer(state, action)
102β
103 timeTravelClient.emit('snapshot', {
104 state: structuredClone(state),
105 timestamp: Date.now(),
106 label: action.type,
107 })
108}
109β
110// Listen for revert commands from devtools
111timeTravelClient.on('revert', (e) => {
112 state = e.payload.state
113 rerender()
114})
115```
116β
117### Panel Side
118β
119Collect snapshots and provide a slider:
120β
121```tsx
122function TimeTravelPanel() {
123 const [snapshots, setSnapshots] = useState([])
124 const [index, setIndex] = useState(0)
125β
126 useEffect(() => {
127 return timeTravelClient.on('snapshot', (e) => {
128 setSnapshots((prev) => [...prev, e.payload])
129 setIndex((prev) => prev + 1)
130 })
131 }, [])
132β
133 const handleSliderChange = (newIndex) => {
134 setIndex(newIndex)
135 timeTravelClient.emit('revert', { state: snapshots[newIndex].state })
136 }
137β
138 return (
139 <div>
140 <input
141 type="range"
142 min={0}
143 max={snapshots.length - 1}
144 value={index}
145 onChange={(e) => handleSliderChange(Number(e.target.value))}
146 />
147 <p>
148 State at: {snapshots[index]?.label} (
149 {new Date(snapshots[index]?.timestamp).toLocaleTimeString()})
150 </p>
151 <pre>{JSON.stringify(snapshots[index]?.state, null, 2)}</pre>
152 </div>
153 )
154}
155```
156β
157## Best Practices
158β
159- **Keep payloads serializable.** No functions, DOM nodes, or circular references.
160- **Use `structuredClone()` for snapshots** to avoid reference mutations.
161- **Debounce frequent emissions** if needed (e.g., rapid state changes).
162- **Use distinct event suffixes** for observation vs commands (e.g., `state-update` for observation, `set-state` for commands).