Skip to content

Commit 5da8b72

Browse files
committed
Update package dependencies and enhance README.md with new features and installation instructions. Refactor socket connection management in useMySocket to support platform-specific URLs and persistent device IDs.
1 parent cbea2e5 commit 5da8b72

File tree

9 files changed

+403
-192
lines changed

9 files changed

+403
-192
lines changed

README.md

Lines changed: 86 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,122 @@
11
# React Query External Sync
22

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.
44

5-
![rn-dev-tools-server](https://github.com/LovesWorking/LovesWorking/assets/111514077/48ac863f-956f-47ef-9d37-e2606bef91e4)
5+
## Features
66

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
5412

5513
## Installation
5614

57-
### Client
58-
59-
#### 1. Install the package
60-
6115
```bash
6216
npm install react-query-external-sync
17+
# or
18+
yarn add react-query-external-sync
6319
```
6420

65-
#### 2. Connect your react-query to the socket
21+
### React Native Requirements
6622

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:
9724

9825
```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
10029
```
10130

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
10932

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';
11935

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+
});
12142

122-
io.on("connection", client => {
123-
console.log(`'${client.handshake.query.username}' connected`)
124-
})
125-
})
43+
return (
44+
// Your app components
45+
);
46+
}
12647
```
12748

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:
15152

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
15361

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
15563

156-
### 1. Image link
64+
### Custom Platform Detection
15765

158-
https://hub.docker.com/repository/docker/navalex/rq_devtool
66+
If you need to override the built-in platform detection:
15967

160-
### 2. Environment variables
68+
```jsx
69+
import {
70+
useQuerySyncSocket,
71+
setPlatformOverride,
72+
} from "react-query-external-sync";
16173

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" });
16576

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+
});
16783

168-
- You'll also need to open both ports to use this image. We suggest to define those in environment variables.
84+
// ...
85+
}
86+
```
16987

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+
}
181115
```
182116

183-
## Contribution
117+
## License
184118

185-
I welcome contributions, feedback, and bug reports. Feel free to open an issue or pull request on this GitHub repository.
119+
MIT
186120

187121
<br>
188122
<hr>

package-lock.json

Lines changed: 1 addition & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,21 @@
3838
"author": "LovesWorking (https://github.com/LovesWorking)",
3939
"license": "MIT",
4040
"dependencies": {
41-
"fast-equals": "^5.0.1",
4241
"socket.io-client": "^4.7.4"
4342
},
4443
"peerDependencies": {
4544
"@tanstack/react-query": "^4.0.0 || ^5.0.0",
46-
"react": "^18"
45+
"react": "^18 || ^19"
46+
},
47+
"peerDependenciesMeta": {
48+
"react-native": {
49+
"optional": true
50+
},
51+
"@react-native-async-storage/async-storage": {
52+
"optional": true
53+
}
4754
},
4855
"devDependencies": {
49-
"@tanstack/react-query": "^5.66.9",
5056
"@babel/core": "^7.23.9",
5157
"@babel/preset-env": "^7.23.9",
5258
"@babel/preset-react": "^7.23.3",
@@ -55,6 +61,7 @@
5561
"@rollup/plugin-node-resolve": "^15.2.3",
5662
"@rollup/plugin-terser": "^0.4.4",
5763
"@rollup/plugin-typescript": "^11.1.6",
64+
"@tanstack/react-query": "^5.66.9",
5865
"@types/node": "^20.11.16",
5966
"@types/react": "^18.2.55",
6067
"rollup": "^4.9.6",

rollup.config.mjs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ export default {
1212
format: "esm",
1313
},
1414
],
15-
external: [
16-
"react",
17-
"fast-equals",
18-
"socket.io-client",
19-
"@tanstack/react-query",
20-
],
15+
external: ["react", "socket.io-client", "@tanstack/react-query"],
2116
plugins: [terser()],
2217
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export let useSyncQueries: typeof import("./new-sync/useSyncQueries").useSyncQueries;
1+
export let useSyncQueries: typeof import("./new-sync/useSyncQueries").useSyncQueriesExternal;
22
// @ts-ignore process.env.NODE_ENV is defined by metro transform plugins
33
if (process.env.NODE_ENV !== "production") {
44
useSyncQueries = require("./new-sync/useSyncQueries").useSyncQueries;

src/new-sync/User.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export interface User {
22
id: string;
33
deviceName: string;
4+
deviceId?: string; // Optional for backward compatibility
5+
platform?: string; // Device platform (iOS, Android, Web)
46
}

src/new-sync/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Export the main hook
2+
export { useMySocket as useQuerySyncSocket } from "./useMySocket";
3+
4+
// Export platform utilities for advanced customization
5+
export {
6+
getPlatform,
7+
setPlatformOverride,
8+
getStorage,
9+
setCustomStorage,
10+
getPlatformSpecificURL,
11+
type StorageInterface,
12+
} from "./platformUtils";

0 commit comments

Comments
 (0)