|
| 1 | +# Firebase Functions Client Demo |
| 2 | + |
| 3 | +A simple web client demonstrating how to call Firebase Functions (both `onRequest` and `onCall`) using the Firebase JavaScript SDK. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. Start the Firebase emulator with the Dart functions: |
| 8 | + |
| 9 | +```bash |
| 10 | +cd ../basic |
| 11 | +dart pub get |
| 12 | +dart run build_runner build --delete-conflicting-outputs |
| 13 | +firebase emulators:start --only functions |
| 14 | +``` |
| 15 | + |
| 16 | +2. Note the Functions URL from the emulator output (usually `http://127.0.0.1:5001/demo-test/us-central1`). |
| 17 | + |
| 18 | +## Running the Client |
| 19 | + |
| 20 | +You can open `index.html` directly in a browser, or serve it with a local server: |
| 21 | + |
| 22 | +```bash |
| 23 | +# Using Python |
| 24 | +python3 -m http.server 8000 |
| 25 | + |
| 26 | +# Using Node.js (npx) |
| 27 | +npx serve . |
| 28 | + |
| 29 | +# Using PHP |
| 30 | +php -S localhost:8000 |
| 31 | +``` |
| 32 | + |
| 33 | +Then open `http://localhost:8000` in your browser. |
| 34 | + |
| 35 | +## Demos Included |
| 36 | + |
| 37 | +### 1. onRequest - Simple HTTP Endpoint |
| 38 | + |
| 39 | +Calls the `helloWorld` function using a simple `fetch()` request. This demonstrates how `onRequest` functions work like regular HTTP endpoints. |
| 40 | + |
| 41 | +```javascript |
| 42 | +const response = await fetch(`${baseUrl}/helloWorld?name=World`); |
| 43 | +const text = await response.text(); |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. onCall - Callable Function |
| 47 | + |
| 48 | +Calls the `greet` function using the Firebase SDK's `httpsCallable()`. This handles authentication, CORS, and request/response formatting automatically. |
| 49 | + |
| 50 | +```javascript |
| 51 | +import { getFunctions, httpsCallable } from 'firebase/functions'; |
| 52 | + |
| 53 | +const functions = getFunctions(app, 'us-central1'); |
| 54 | +const greet = httpsCallable(functions, 'greet'); |
| 55 | +const result = await greet({ name: 'Dart Developer' }); |
| 56 | +// result.data = { message: 'Hello, Dart Developer!' } |
| 57 | +``` |
| 58 | + |
| 59 | +### 3. onCall - Error Handling |
| 60 | + |
| 61 | +Demonstrates how callable functions handle errors. The `divide` function throws structured errors for invalid input or division by zero. |
| 62 | + |
| 63 | +### 4. onCallWithData - Typed Callable |
| 64 | + |
| 65 | +Calls the `greetTyped` function which uses typed request/response classes on the server side. |
| 66 | + |
| 67 | +### 5. Raw POST to Callable |
| 68 | + |
| 69 | +Shows how to call a callable function without the Firebase SDK using the callable protocol: |
| 70 | + |
| 71 | +```javascript |
| 72 | +const response = await fetch(`${baseUrl}/greet`, { |
| 73 | + method: 'POST', |
| 74 | + headers: { 'Content-Type': 'application/json' }, |
| 75 | + body: JSON.stringify({ data: { name: 'Raw Caller' } }) |
| 76 | +}); |
| 77 | +const json = await response.json(); |
| 78 | +// json = { result: { message: 'Hello, Raw Caller!' } } |
| 79 | +``` |
| 80 | + |
| 81 | +## Callable Protocol |
| 82 | + |
| 83 | +The Firebase callable protocol wraps data in a specific format: |
| 84 | + |
| 85 | +**Request:** |
| 86 | +```json |
| 87 | +{ |
| 88 | + "data": { /* your input */ } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +**Response:** |
| 93 | +```json |
| 94 | +{ |
| 95 | + "result": { /* function return value */ } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +**Error:** |
| 100 | +```json |
| 101 | +{ |
| 102 | + "error": { |
| 103 | + "status": "INVALID_ARGUMENT", |
| 104 | + "message": "Error description" |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Troubleshooting |
| 110 | + |
| 111 | +- **CORS errors**: Make sure you're running the Firebase emulator and the Functions URL is correct. |
| 112 | +- **Connection refused**: Ensure the emulator is running on the expected port. |
| 113 | +- **Function not found**: Verify the function names match between client and server. |
0 commit comments