Skip to content

Commit 187061f

Browse files
authored
Merge pull request #756 from ZenUml/fix/track-endpoint-timeout
feat(docs): add Firebase Emulator Testing Guide and update firebase.j…
2 parents 1d37a25 + 296b98d commit 187061f

File tree

4 files changed

+190
-5
lines changed

4 files changed

+190
-5
lines changed

docs/firebase-emulator-testing.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Firebase Emulator Testing Guide
2+
3+
This guide explains how to run the Firebase emulator locally and test your Firebase functions using cURL.
4+
5+
## Running the Firebase Emulator
6+
7+
1. Navigate to your project root directory:
8+
```bash
9+
cd /Users/pengxiao/workspaces/zenuml/web-sequence
10+
```
11+
12+
2. Start the Firebase emulator:
13+
```bash
14+
firebase emulators:start
15+
```
16+
17+
This will start the emulator and display the available endpoints in the terminal.
18+
19+
## Accessing the Emulator UI
20+
21+
The Firebase Emulator Suite includes a web-based UI that provides a dashboard for all your emulated services:
22+
23+
1. When you start the emulators, look for a message like:
24+
```
25+
✔ emulators: All emulators ready! It is now safe to connect.
26+
i emulators: View Emulator UI at http://localhost:4000/
27+
```
28+
29+
2. Open the provided URL in your browser (typically http://localhost:4000)
30+
31+
3. The UI provides:
32+
- A list of all your Cloud Functions endpoints
33+
- The ability to view logs for each function
34+
- A way to test functions directly from the UI
35+
- Firestore database viewer (if enabled)
36+
- Authentication emulator (if enabled)
37+
38+
This UI makes it easier to debug and test your Firebase functions without having to use cURL commands for everything.
39+
40+
## Testing Firebase Functions with cURL
41+
42+
### Testing the Info Endpoint
43+
44+
To verify that the emulator is running correctly, test the `/info` endpoint:
45+
46+
```bash
47+
curl -X POST \
48+
http://localhost:5000/info \
49+
-H 'Content-Type: application/json' \
50+
-d '{"event": {"event": "testEvent", "category": "test"}, "userId": "test-user"}'
51+
```
52+
53+
You should receive a response like:
54+
```
55+
Hello from web-sequence-local!
56+
```
57+
58+
### Testing the Track Endpoint
59+
60+
To test the `/track` endpoint:
61+
62+
```bash
63+
curl -X POST \
64+
http://localhost:5000/track \
65+
-H 'Content-Type: application/json' \
66+
-d '{"event": {"event": "testEvent", "category": "test"}, "userId": "test-user"}'
67+
```
68+
69+
You should receive a response like:
70+
```
71+
Event tracked successfully
72+
```
73+
74+
### Testing Different Payload Formats
75+
76+
The track endpoint supports two different payload formats:
77+
78+
1. Object format (recommended):
79+
```bash
80+
curl -X POST \
81+
http://localhost:5002/track \
82+
-H 'Content-Type: application/json' \
83+
-d '{"event": {"event": "testEvent", "category": "test"}, "userId": "test-user"}'
84+
```
85+
86+
2. String format (legacy):
87+
```bash
88+
curl -X POST \
89+
http://localhost:5002/track \
90+
-H 'Content-Type: application/json' \
91+
-d '{"event": "testEvent", "category": "test", "userId": "test-user"}'
92+
```
93+
94+
## Monitoring Logs
95+
96+
While the emulator is running, you can monitor the logs in the terminal. These logs will show:
97+
98+
1. Incoming requests
99+
2. Function execution details
100+
3. Any errors or warnings
101+
4. Mixpanel tracking information
102+
103+
## Troubleshooting
104+
105+
### Functions Emulator Shows as "OFF" in the UI
106+
107+
If the Functions emulator shows as "OFF" in the Emulator UI:
108+
109+
1. Make sure your `firebase.json` file has the proper emulators configuration:
110+
```json
111+
"emulators": {
112+
"functions": {
113+
"port": 5002
114+
},
115+
"hosting": {
116+
"port": 5000
117+
},
118+
"ui": {
119+
"enabled": true,
120+
"port": 4000
121+
}
122+
}
123+
```
124+
125+
2. Try starting the emulator with the explicit `--only` flag:
126+
```bash
127+
firebase emulators:start --only functions
128+
```
129+
130+
3. Check the terminal output for any error messages related to the Functions emulator.
131+
132+
### Timeout Issues with the Track Endpoint
133+
134+
If you encounter a timeout issue with the `/track` endpoint:
135+
136+
1. Check the emulator logs for any errors
137+
2. Verify that the function is responding immediately without waiting for Mixpanel
138+
3. Make sure the payload format is correct
139+
140+
## Notes on URL Structure
141+
142+
- Direct function URLs: `http://localhost:5002/{functionName}`
143+
- If you've added rewrite rules in firebase.json, you can also use: `http://localhost:5000/{path}`
144+
145+
Remember to stop the emulator when you're done testing by pressing `Ctrl+C` in the terminal.

firebase.json

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"hosting": {
33
"public": "app",
4-
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
4+
"ignore": [
5+
"firebase.json",
6+
"**/.*",
7+
"**/node_modules/**"
8+
],
59
"rewrites": [
610
{
711
"source": "/sync-diagram",
@@ -20,11 +24,45 @@
2024
"function": {
2125
"functionId": "track"
2226
}
27+
},
28+
{
29+
"source": "/info",
30+
"function": {
31+
"functionId": "info"
32+
}
2333
}
2434
]
2535
},
2636
"firestore": {
2737
"rules": "firestore.rules",
2838
"indexes": "firestore.indexes.json"
29-
}
39+
},
40+
"emulators": {
41+
"functions": {
42+
"port": 5002
43+
},
44+
"firestore": {
45+
"port": 8080
46+
},
47+
"hosting": {
48+
"port": 5000
49+
},
50+
"ui": {
51+
"enabled": true,
52+
"port": 4000
53+
}
54+
},
55+
"functions": [
56+
{
57+
"source": "functions",
58+
"codebase": "default",
59+
"ignore": [
60+
"node_modules",
61+
".git",
62+
"firebase-debug.log",
63+
"firebase-debug.*.log",
64+
"*.local"
65+
]
66+
}
67+
]
3068
}

functions/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
*.local

functions/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const mixpanel = Mixpanel.init('0c62cea9ed2247f4824bf196f6817941');
1010

1111
const webhook = require('./webhook');
1212
const alertParser = require('./alert_parser');
13-
const pubKey = functions.config().paddle.pub_key;
13+
const pubKey = functions.config().paddle?.pub_key;
1414
const https = require('https');
1515

1616
exports.info = functions.https.onRequest((req, res) => {
@@ -19,7 +19,7 @@ exports.info = functions.https.onRequest((req, res) => {
1919

2020
const verifyIdToken = (token) => admin.auth().verifyIdToken(token);
2121

22-
const supportedProductIds = (functions.config().paddle.product_ids || '')
22+
const supportedProductIds = (functions.config().paddle?.product_ids || '')
2323
.split(',')
2424
.filter(Boolean);
2525
const checkSupportedProductIds = (productId) => {
@@ -125,7 +125,7 @@ exports.track = functions.https.onRequest(async (req, res) => {
125125
event_label: req.body.label,
126126
displayProductName: 'FireWeb',
127127
});
128-
128+
129129
// Send a success response to prevent 502 Bad Gateway error
130130
res.status(200).send('Event tracked successfully');
131131
});

0 commit comments

Comments
 (0)