|
1 | 1 | # React Query External Sync |
2 | 2 |
|
3 | | -React Query devtool synced with Socket.IO, to debug your react-query outstide your app easily. |
| 3 | +A tool for syncing React Query state to an external React Query Dev Tools instance. |
4 | 4 |
|
5 | | - |
| 5 | +## Features |
6 | 6 |
|
7 | | -## Table of content |
8 | | - |
9 | | -- [Introduction](#introduction) |
10 | | - - [Key Advantages](#key-advantages) |
11 | | -- [Prerequisites](#prerequisites) |
12 | | - - [Client Application](#client-application) |
13 | | - - [Socket IO Server](#socket-io-server) |
14 | | - - [Devtool Website](#devtool-website) |
15 | | -- [Installation](#installation) |
16 | | - + [Client](#client) |
17 | | - - [1. Install the package](#1-install-the-package) |
18 | | - - [2. Connect your react-query to the socket](#2-connect-your-react-query-to-the-socket) |
19 | | - - [3. Usages](#3-usages) |
20 | | - + [Socket IO Server](#socket-io-server) |
21 | | - - [1. Install the package](#1-install-the-package-1) |
22 | | - - [2. Setup Socket IO](#2-setup-socket-io) |
23 | | - + [React Query Dev Tools Server](#react-query-dev-tools-server) |
24 | | -- [Ready to use Docker image](#ready-to-use-docker-image) |
25 | | - - [1. Image link](#1-image-link) |
26 | | - - [2. Environment variables](#2-environment-variables) |
27 | | - - [3. Docker Compose example](#3-docker-compose-example) |
28 | | -- [Contribution](#contribution) |
29 | | - |
30 | | -## Introduction |
31 | | - |
32 | | -**React Query External Sync** is a dynamic tool for managing React Query state outside the usual confines of React Query Dev Tools. Ideal for React projects, it offers a live state management solution that's accessible to both developers, qa and non-technical team members. |
33 | | - |
34 | | -### Key Advantages: |
35 | | -- **Real-time UI Updates**: Instantly see how state changes affect your application's UI without backend alterations. |
36 | | -- **Broad Accessibility**: Enables all team members, including QA, designers, and business professionals, to tweak and test API-driven UI components on the fly. |
37 | | -- **Continuous Evolution**: Built with expansion in mind, expect regular feature updates driven by community feedback and the evolving needs of modern development workflows. |
38 | | -- **Enhanced Manipulation**: Future updates will introduce capabilities for precise state adjustments, such as directly inserting complete objects or arrays, object duplication, simultaneous state syncing across web, Android, and iOS and persistent state overrides, allowing values for specific data to remain until manually reverted. |
39 | | - |
40 | | -## Prerequisites |
41 | | - |
42 | | -### Client Application |
43 | | - |
44 | | -- React version 18 or above. |
45 | | -- React Query version 5.17.19 or above. |
46 | | - |
47 | | -### Socket IO Server |
48 | | - |
49 | | -- Socket.io version 4.7.4 or above. |
50 | | - |
51 | | -### Devtool Website |
52 | | - |
53 | | -- Any react.js ready server (vite, rca, ...) |
| 7 | +- Works in both React Native and browser/web environments |
| 8 | +- Syncs React Query state between your app and external dev tools |
| 9 | +- Automatically detects platform (iOS, Android, Web, etc.) |
| 10 | +- Handles platform-specific networking (like Android emulator's 10.0.2.2 for localhost) |
| 11 | +- Uses appropriate storage mechanisms based on environment |
54 | 12 |
|
55 | 13 | ## Installation |
56 | 14 |
|
57 | | -### Client |
58 | | - |
59 | | -#### 1. Install the package |
60 | | - |
61 | 15 | ```bash |
62 | 16 | npm install react-query-external-sync |
| 17 | +# or |
| 18 | +yarn add react-query-external-sync |
63 | 19 | ``` |
64 | 20 |
|
65 | | -#### 2. Connect your react-query to the socket |
| 21 | +### React Native Requirements |
66 | 22 |
|
67 | | -- Import the useAllQueries hook and utilize it within your client application to enable real-time synchronization with the external dashboard. |
68 | | - |
69 | | -```javascript |
70 | | -import { useAllQueries } from "react-query-external-sync"; |
71 | | - |
72 | | -const { connect, disconnect, isConnected, queries, socket, users } = |
73 | | - useAllQueries({ |
74 | | - query: { |
75 | | - username: "myUsername", |
76 | | - userType: "User", // Role of the user |
77 | | - clientType: "client", // client | server |
78 | | - }, |
79 | | - queryClient, |
80 | | - socketURL: "http://localhost:4000", |
81 | | - }); |
82 | | -``` |
83 | | - |
84 | | -- **query**: Contains user information for identifying and managing connections in the dashboard. |
85 | | -- **queryClient**: Your application's React Query client instance. |
86 | | -- **socketURL**: The URL where your Socket.io server is hosted. |
87 | | - |
88 | | -#### 3. Usages |
89 | | - |
90 | | -- `.connect()`: initiate a connection with the socket server |
91 | | -- `.disconnect()`: terminate the connection to the socket server by invoking the disconnect function |
92 | | -- `.isConnected`: monitor the connection status |
93 | | - |
94 | | -### Socket IO Server |
95 | | - |
96 | | -#### 1. Install the package |
| 23 | +If you're using this in a React Native project, you'll need to install the following optional dependencies: |
97 | 24 |
|
98 | 25 | ```bash |
99 | | -npm install react-query-external-dash |
| 26 | +npm install react-native @react-native-async-storage/async-storage |
| 27 | +# or |
| 28 | +yarn add react-native @react-native-async-storage/async-storage |
100 | 29 | ``` |
101 | 30 |
|
102 | | -#### 2. Setup Socket IO |
103 | | - |
104 | | -- After setting up your Socket.io server, integrate the socketHandle function to manage incoming and outgoing messages related to query state synchronization. |
105 | | -- **Basic socket io Nodejs server example**: |
106 | | - |
107 | | -```javascript |
108 | | -import { socketHandle } from "react-query-external-dash" |
| 31 | +## Usage |
109 | 32 |
|
110 | | -import("socket.io").then(4000 => { |
111 | | - const io = new socketIO.Server(socketPort, { |
112 | | - cors: { |
113 | | - // This origin is the devtool (see next section), change the port to fit your needs. |
114 | | - // You'll also need to add the URL of your client if you have any CORS issue |
115 | | - origin: ["http://localhost:4001"], |
116 | | - credentials: true, |
117 | | - }, |
118 | | - }) |
| 33 | +```jsx |
| 34 | +import { useQuerySyncSocket } from 'react-query-external-sync'; |
119 | 35 |
|
120 | | - socketHandle({ io }) |
| 36 | +function App() { |
| 37 | + // Set up the sync socket |
| 38 | + useQuerySyncSocket({ |
| 39 | + deviceName: 'MyApp', // Name to identify this device |
| 40 | + socketURL: 'http://localhost:42831', // URL to your dev tools instance |
| 41 | + }); |
121 | 42 |
|
122 | | - io.on("connection", client => { |
123 | | - console.log(`'${client.handshake.query.username}' connected`) |
124 | | - }) |
125 | | -}) |
| 43 | + return ( |
| 44 | + // Your app components |
| 45 | + ); |
| 46 | +} |
126 | 47 | ``` |
127 | 48 |
|
128 | | -### React Query Dev Tools Server |
129 | | - |
130 | | -- Incorporate the ExternalDevTools component within any React.JS ready server |
131 | | -- **Basic react-vite server example** _(we suppose here that the port is 4001)_: |
132 | | - |
133 | | -```javascript |
134 | | -import React from "react" |
135 | | -import ReactDOM from "react-dom/client" |
136 | | -import { ExternalDevTools } from "react-query-external-dash" |
137 | | - |
138 | | -ReactDOM.createRoot(document.getElementById("root")!).render( |
139 | | - <React.StrictMode> |
140 | | - <ExternalDevTools |
141 | | - socketURL={"http://localhost:4000"} |
142 | | - query={{ |
143 | | - clientType: "server", |
144 | | - username: "Admin", |
145 | | - userType: "admin", |
146 | | - }} |
147 | | - /> |
148 | | - </React.StrictMode>, |
149 | | -) |
150 | | -``` |
| 49 | +## How It Works |
| 50 | + |
| 51 | +This package provides a cross-platform solution for syncing React Query state to external tools: |
151 | 52 |
|
152 | | -## Ready to use Docker image |
| 53 | +1. **Platform Detection**: Automatically detects if running in React Native or web environment |
| 54 | +2. **Storage Abstraction**: Uses the appropriate storage mechanism: |
| 55 | + - React Native: AsyncStorage (if available) |
| 56 | + - Web: localStorage |
| 57 | + - Fallback: In-memory storage |
| 58 | +3. **Network Configuration**: Handles platform-specific networking requirements |
| 59 | + - For Android emulators: Maps localhost to 10.0.2.2 |
| 60 | + - For other environments: Uses the provided URL |
153 | 61 |
|
154 | | -If you don't want to setup both Socket.IO + Dedicated React.js server to monitor your app, a Docker image is available to launch those both services at once, with custom ports and CORS urls. |
| 62 | +## Advanced Configuration |
155 | 63 |
|
156 | | -### 1. Image link |
| 64 | +### Custom Platform Detection |
157 | 65 |
|
158 | | -https://hub.docker.com/repository/docker/navalex/rq_devtool |
| 66 | +If you need to override the built-in platform detection: |
159 | 67 |
|
160 | | -### 2. Environment variables |
| 68 | +```jsx |
| 69 | +import { |
| 70 | + useQuerySyncSocket, |
| 71 | + setPlatformOverride, |
| 72 | +} from "react-query-external-sync"; |
161 | 73 |
|
162 | | -- `SOCKET_PORT`: Port for the Socket.io server |
163 | | -- `DT_PORT`: Port for the Vite server to access your devtool |
164 | | -- `CORS_ORIGINS`: String to specify authorized url's for CORS in form of: "url1,url2,url3,..." (separate with coma without spaces). **Note that the devtool url is automaticly included in the CORS Policy.** |
| 74 | +// Set a custom platform |
| 75 | +setPlatformOverride({ os: "customOS", name: "Custom Platform" }); |
165 | 76 |
|
166 | | -### 3. Docker Compose example |
| 77 | +// Use normally after setting override |
| 78 | +function App() { |
| 79 | + useQuerySyncSocket({ |
| 80 | + deviceName: "MyCustomPlatformApp", |
| 81 | + socketURL: "http://localhost:3000", |
| 82 | + }); |
167 | 83 |
|
168 | | -- You'll also need to open both ports to use this image. We suggest to define those in environment variables. |
| 84 | + // ... |
| 85 | +} |
| 86 | +``` |
169 | 87 |
|
170 | | -```yaml |
171 | | -services: |
172 | | - rqDevtools: |
173 | | - image: navalex/rq_devtool:latest |
174 | | - ports: |
175 | | - - ${RQ_DEVTOOLS_PORT}:${RQ_DEVTOOLS_PORT} |
176 | | - - ${RQ_DEVTOOLS_SOCKET_PORT}:${RQ_DEVTOOLS_SOCKET_PORT} |
177 | | - environment: |
178 | | - DT_PORT: ${RQ_DEVTOOLS_PORT} |
179 | | - SOCKET_PORT: ${RQ_DEVTOOLS_SOCKET_PORT} |
180 | | - SOCKET_CORS: "http://localhost:8102,http://localhost:5173" |
| 88 | +### Custom Storage |
| 89 | + |
| 90 | +If you need to provide a custom storage implementation: |
| 91 | + |
| 92 | +```jsx |
| 93 | +import { |
| 94 | + useQuerySyncSocket, |
| 95 | + setCustomStorage, |
| 96 | +} from "react-query-external-sync"; |
| 97 | + |
| 98 | +// Set a custom storage implementation |
| 99 | +setCustomStorage({ |
| 100 | + getItem: async (key) => { |
| 101 | + /* your implementation */ |
| 102 | + }, |
| 103 | + setItem: async (key, value) => { |
| 104 | + /* your implementation */ |
| 105 | + }, |
| 106 | + removeItem: async (key) => { |
| 107 | + /* your implementation */ |
| 108 | + }, |
| 109 | +}); |
| 110 | + |
| 111 | +// Use normally after setting custom storage |
| 112 | +function App() { |
| 113 | + // ... |
| 114 | +} |
181 | 115 | ``` |
182 | 116 |
|
183 | | -## Contribution |
| 117 | +## License |
184 | 118 |
|
185 | | -I welcome contributions, feedback, and bug reports. Feel free to open an issue or pull request on this GitHub repository. |
| 119 | +MIT |
186 | 120 |
|
187 | 121 | <br> |
188 | 122 | <hr> |
|
0 commit comments