Skip to content

Commit b412e13

Browse files
committed
initial commit
0 parents  commit b412e13

File tree

9 files changed

+6429
-0
lines changed

9 files changed

+6429
-0
lines changed

.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Roboflow API Configuration
2+
# Get your API key from: https://app.roboflow.com/settings/api
3+
4+
ROBOFLOW_API_KEY=your_api_key_here
5+
6+
# Optional: Custom Roboflow server URL
7+
# ROBOFLOW_SERVER_URL=https://serverless.roboflow.one
8+
9+
# Server Port (optional, defaults to 3000)
10+
# PORT=3000

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Environment variables
5+
.env
6+
.env.local
7+
8+
# Build output
9+
public/
10+
dist/
11+
12+
# Logs
13+
*.log
14+
npm-debug.log*
15+
16+
# OS files
17+
.DS_Store
18+
Thumbs.db
19+
20+
# IDE
21+
.vscode/
22+
.idea/
23+
*.swp
24+
*.swo

README.md

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
# Roboflow WebRTC Secure Streaming Example
2+
3+
A production-ready example of real-time video streaming with Roboflow using WebRTC. This example demonstrates the **secure proxy pattern** where your API key stays on the backend server and is never exposed to the browser.
4+
5+
## Features
6+
7+
- 🔒 **Secure**: API key kept on backend, never exposed to frontend
8+
- 🚀 **Production-Ready**: Proper error handling, health checks, and logging
9+
- 📦 **Modern Stack**: Express backend + Vite frontend + InferenceJS from npm
10+
- 🎥 **Real-Time**: WebRTC streaming for low-latency computer vision
11+
- 🎨 **Instance Segmentation**: Demo workflow detecting and segmenting objects
12+
13+
## Architecture
14+
15+
```
16+
Browser Your Server Roboflow API
17+
┌─────────┐ ┌──────────┐ ┌──────────┐
18+
│ │ WebRTC │ │ WebRTC │ │
19+
│ Client │◄─────────────►│ Proxy │◄─────────────►│ Serverless│
20+
│ │ Offer │ │ + API Key │ │
21+
└─────────┘ └──────────┘ └──────────┘
22+
```
23+
24+
The frontend uses `connectors.withProxyUrl('/api/init-webrtc')` which sends the WebRTC offer to your backend. Your backend adds the secret API key and forwards to Roboflow, keeping credentials secure.
25+
26+
## Quick Start
27+
28+
### 1. Install Dependencies
29+
30+
```bash
31+
npm install
32+
```
33+
34+
This will install:
35+
- `inferencejs` from GitHub (latest version)
36+
- Express server dependencies
37+
- Vite for frontend bundling
38+
39+
### 2. Configure API Key
40+
41+
Get your Roboflow API key from [https://app.roboflow.com/settings/api](https://app.roboflow.com/settings/api)
42+
43+
Create a `.env` file:
44+
45+
```bash
46+
cp .env.example .env
47+
```
48+
49+
Edit `.env` and add your API key:
50+
51+
```env
52+
ROBOFLOW_API_KEY=your_api_key_here
53+
```
54+
55+
### 3. Run in Development Mode
56+
57+
```bash
58+
npm run dev
59+
```
60+
61+
This runs both:
62+
- Vite dev server on `http://localhost:5173` (frontend with HMR)
63+
- Express API server on `http://localhost:3000` (backend proxy)
64+
65+
Open [http://localhost:5173](http://localhost:5173) in your browser.
66+
67+
### 4. Build for Production
68+
69+
```bash
70+
npm run build
71+
npm start
72+
```
73+
74+
- `npm run build` compiles the frontend to `public/`
75+
- `npm start` runs Express in production mode, serving built files
76+
77+
Open [http://localhost:3000](http://localhost:3000) in your browser.
78+
79+
## Project Structure
80+
81+
```
82+
sampleApp/
83+
├── server.js # Express backend with /api/init-webrtc proxy
84+
├── vite.config.js # Vite build configuration
85+
├── package.json # Dependencies and scripts
86+
├── .env # Your API key (create from .env.example)
87+
├── .env.example # Template for environment variables
88+
├── .gitignore # Git ignore rules
89+
├── src/
90+
│ ├── index.html # Frontend UI
91+
│ └── app.js # Frontend logic (imports from npm)
92+
└── public/ # Built frontend (auto-generated)
93+
```
94+
95+
## API Endpoints
96+
97+
### `POST /api/init-webrtc`
98+
99+
Proxies WebRTC initialization to Roboflow.
100+
101+
**Request:**
102+
```json
103+
{
104+
"offer": {
105+
"sdp": "...",
106+
"type": "offer"
107+
},
108+
"wrtcparams": {
109+
"workflowSpec": { ... },
110+
"imageInputName": "image",
111+
"streamOutputNames": ["output_image"]
112+
}
113+
}
114+
```
115+
116+
**Response:**
117+
```json
118+
{
119+
"sdp": "...",
120+
"type": "answer",
121+
"context": {
122+
"pipeline_id": "abc123",
123+
"request_id": "xyz789"
124+
}
125+
}
126+
```
127+
128+
### `GET /api/health`
129+
130+
Health check endpoint.
131+
132+
**Response:**
133+
```json
134+
{
135+
"status": "ok",
136+
"apiKeyConfigured": true,
137+
"message": "Server is ready"
138+
}
139+
```
140+
141+
## Deployment
142+
143+
### Deploy to Vercel
144+
145+
1. Install Vercel CLI:
146+
```bash
147+
npm i -g vercel
148+
```
149+
150+
2. Deploy:
151+
```bash
152+
vercel
153+
```
154+
155+
3. Set environment variable in Vercel dashboard:
156+
- Go to your project settings
157+
- Add `ROBOFLOW_API_KEY` environment variable
158+
- Redeploy
159+
160+
### Deploy to Heroku
161+
162+
1. Install Heroku CLI and login:
163+
```bash
164+
heroku login
165+
```
166+
167+
2. Create app:
168+
```bash
169+
heroku create your-app-name
170+
```
171+
172+
3. Set environment variable:
173+
```bash
174+
heroku config:set ROBOFLOW_API_KEY=your_api_key_here
175+
```
176+
177+
4. Deploy:
178+
```bash
179+
git push heroku main
180+
```
181+
182+
### Deploy to Any VPS (DigitalOcean, AWS, etc.)
183+
184+
1. SSH into your server
185+
2. Clone your repository
186+
3. Install Node.js (v18+)
187+
4. Create `.env` file with your API key
188+
5. Run:
189+
```bash
190+
npm install
191+
npm run build
192+
npm start
193+
```
194+
195+
6. Use PM2 for process management:
196+
```bash
197+
npm install -g pm2
198+
pm2 start server.js --name roboflow-webrtc
199+
pm2 save
200+
pm2 startup
201+
```
202+
203+
7. Setup nginx as reverse proxy (optional):
204+
```nginx
205+
server {
206+
listen 80;
207+
server_name yourdomain.com;
208+
209+
location / {
210+
proxy_pass http://localhost:3000;
211+
proxy_http_version 1.1;
212+
proxy_set_header Upgrade $http_upgrade;
213+
proxy_set_header Connection 'upgrade';
214+
proxy_set_header Host $host;
215+
proxy_cache_bypass $http_upgrade;
216+
}
217+
}
218+
```
219+
220+
### Deploy with Docker
221+
222+
1. Create `Dockerfile`:
223+
```dockerfile
224+
FROM node:18-alpine
225+
WORKDIR /app
226+
COPY package*.json ./
227+
RUN npm ci --production
228+
COPY . .
229+
RUN npm run build
230+
EXPOSE 3000
231+
CMD ["node", "server.js"]
232+
```
233+
234+
2. Create `.dockerignore`:
235+
```
236+
node_modules
237+
.env
238+
public
239+
.git
240+
```
241+
242+
3. Build and run:
243+
```bash
244+
docker build -t roboflow-webrtc .
245+
docker run -p 3000:3000 -e ROBOFLOW_API_KEY=your_key roboflow-webrtc
246+
```
247+
248+
## Environment Variables
249+
250+
| Variable | Required | Default | Description |
251+
|----------|----------|---------|-------------|
252+
| `ROBOFLOW_API_KEY` | ✅ Yes | - | Your Roboflow API key |
253+
| `ROBOFLOW_SERVER_URL` | ❌ No | `https://serverless.roboflow.one` | Custom Roboflow server URL |
254+
| `PORT` | ❌ No | `3000` | Server port |
255+
256+
## Security Best Practices
257+
258+
**DO:**
259+
- Keep `.env` file in `.gitignore`
260+
- Use environment variables for API keys
261+
- Use HTTPS in production
262+
- Validate requests on the backend
263+
- Rate limit your API endpoints
264+
- Use CORS properly
265+
266+
**DON'T:**
267+
- Commit API keys to git
268+
- Expose API keys in frontend code
269+
- Use `connectors.withApiKey()` in production
270+
- Skip input validation
271+
- Allow unrestricted access to proxy endpoint
272+
273+
## Customizing the Workflow
274+
275+
Edit `src/app.js` to change the workflow specification:
276+
277+
```javascript
278+
const WORKFLOW_SPEC = {
279+
version: "1.0",
280+
inputs: [
281+
{
282+
type: "InferenceImage",
283+
name: "image"
284+
}
285+
],
286+
steps: [
287+
{
288+
type: "roboflow_core/roboflow_object_detection_model@v2",
289+
name: "model",
290+
images: "$inputs.image",
291+
model_id: "your-project/your-version"
292+
}
293+
// Add more steps...
294+
],
295+
outputs: [
296+
{
297+
type: "JsonField",
298+
name: "output_image",
299+
selector: "$steps.model.predictions"
300+
}
301+
]
302+
};
303+
```
304+
305+
Learn more about workflows: [https://docs.roboflow.com/workflows](https://docs.roboflow.com/workflows)
306+
307+
## Troubleshooting
308+
309+
### "Server configuration error: API key not configured"
310+
311+
Make sure you created a `.env` file with `ROBOFLOW_API_KEY`:
312+
```bash
313+
cp .env.example .env
314+
# Edit .env and add your API key
315+
```
316+
317+
### "Failed to initialize WebRTC worker"
318+
319+
Check server logs for errors. Common issues:
320+
- Invalid API key
321+
- Network connectivity issues
322+
- Invalid workflow specification
323+
324+
### Camera not working
325+
326+
- Check browser permissions
327+
- Use HTTPS in production (required for camera access)
328+
- Try a different browser
329+
330+
### Vite dev mode: "Failed to fetch dynamically imported module"
331+
332+
Make sure both servers are running:
333+
```bash
334+
npm run dev # Runs both Vite and Express
335+
```
336+
337+
## Learn More
338+
339+
- [Roboflow Documentation](https://docs.roboflow.com)
340+
- [InferenceJS GitHub](https://github.com/roboflow/inferencejs)
341+
- [Workflows Documentation](https://docs.roboflow.com/workflows)
342+
- [WebRTC API Reference](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API)
343+
344+
## License
345+
346+
MIT
347+
348+
## Support
349+
350+
- GitHub Issues: [https://github.com/roboflow/inferencejs/issues](https://github.com/roboflow/inferencejs/issues)
351+
- Roboflow Forum: [https://discuss.roboflow.com](https://discuss.roboflow.com)
352+

0 commit comments

Comments
 (0)