Package Exports
- @supernick135/face-scanner-client
- @supernick135/face-scanner-client/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@supernick135/face-scanner-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
π Face Scanner WebSocket Client Library
A comprehensive Node.js client library for integrating with Face Scanner WebSocket servers using secure API key authentication.
β¨ Features
- π Secure API Key Authentication - Replace password-based auth with scoped API keys
- π₯ Multiple Client Types - Support for student, device, monitor, service, and admin clients
- π Auto-Reconnection - Intelligent reconnection with exponential backoff
- π‘ Real-time Events - Face scan events, device status, and system alerts
- π Smart Room Management - Automatic room joining based on client type
- β€οΈ Health Monitoring - Built-in ping/pong heartbeat mechanism
- π‘οΈ Security Features - Rate limiting, scoped permissions, audit logging
π Quick Start
Installation
git clone https://github.com/Narakornnick135/lib-facescan-ws.git
cd lib-facescan-ws
npm installConfiguration
Copy the environment template:
cp .env.example .envUpdate .env with your configuration:
# WebSocket Server
WS_URL=ws://localhost:8080/ws
# Your API tokens (get from admin interface)
STUDENT_TOKEN=your-student-token-here
DEVICE_TOKEN=your-device-token-here
# Client Information
STUDENT_ID=your-student-id
SCHOOL_ID=your-school-idBasic Usage
Student Client
const WebSocketClient = require('./lib/client/WebSocketClient');
const studentClient = WebSocketClient.createStudentClient({
wsUrl: process.env.WS_URL,
apiKey: process.env.STUDENT_TOKEN,
schoolId: process.env.SCHOOL_ID,
studentId: process.env.STUDENT_ID
});
studentClient.on('authenticated', () => {
console.log('β
Connected and ready to receive notifications');
});
studentClient.on('faceDetected', (data) => {
console.log('π€ Your face was detected!', data);
});
await studentClient.connect();Device Client
const deviceClient = WebSocketClient.createDeviceClient({
wsUrl: process.env.WS_URL,
apiKey: process.env.DEVICE_TOKEN,
schoolId: process.env.SCHOOL_ID,
deviceId: process.env.DEVICE_ID
});
// Send face scan event
deviceClient.sendFaceScanEvent('face_detected', {
studentId: 'student123',
confidence: 0.95,
timestamp: Date.now()
});π Client Types
| Client Type | Purpose | Authentication | Permissions |
|---|---|---|---|
| Student | Receive personal notifications | API Key | Own events only |
| Device | Face scanning devices | API Key | Send scan events |
| Monitor | Real-time dashboards | API Key | School monitoring |
| Service | Backend integrations | API Key | Cross-school events |
| Admin | System management | JWT Token | Full system access |
π§ͺ Testing
Run Tests
# Individual tests
npm run testπ§βπsimple
npm run testπ§βπlisten
# Direct execution
node examples/student-client.js
node tests/test-student-simple.jsRun All Examples
# Student notifications
node examples/student-client.js
# Device scanner simulation
node examples/device-client.js
# Real-time monitoring
node examples/monitor-client.js
# Complete integration demo
node examples/complete-integration.jsπ Documentation
- Complete Test Report - Comprehensive testing guide
- Testing Guide - Step-by-step testing instructions
- Examples README - All example implementations
- Migration Guide - Upgrade from password auth
π§ API Reference
WebSocketClient Methods
// Factory methods for different client types
WebSocketClient.createStudentClient(options)
WebSocketClient.createDeviceClient(options)
WebSocketClient.createMonitorClient(options)
WebSocketClient.createServiceClient(options)
WebSocketClient.createAdminClient(options)
// Connection management
await client.connect()
client.disconnect()
client.getConnectionStatus()
// Messaging
client.send(message)
client.sendPingMessage()
client.sendFaceScanEvent(type, data) // Device clients
// Room management (non-student clients)
client.joinRoom(roomName)
client.leaveRoom(roomName)
// Admin functions
client.requestStats() // Admin only
client.kickClient(clientId, reason) // Admin onlyEvents
// Connection events
client.on('connected', () => {})
client.on('authenticated', () => {})
client.on('disconnected', ({ code, reason }) => {})
client.on('authError', (error) => {})
// Face scan events
client.on('faceDetected', (data) => {})
client.on('scanComplete', (data) => {})
client.on('noFace', (data) => {})
client.on('faceError', (data) => {})
// System events
client.on('systemAlert', (data) => {})
client.on('roomUpdate', (data) => {})
client.on('pong', () => {})π Security Features
API Key Scoping
- Each client type has specific permissions
- Keys can be scoped to specific schools/devices
- Automatic expiration support
Student Privacy Protection
- Students only receive events for their own ID
- Cannot manually join other rooms
- All connections logged for audit
Rate Limiting
- Automatic rate limiting per API key
- Configurable limits per client type
- Prevents abuse and DoS attacks
ποΈ Architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Student App β β Device Scanner β β Dashboard β
β β β β β β
βββββββββββ¬ββββββββ βββββββββββ¬ββββββββ βββββββββββ¬ββββββββ
β β β
β Student Client β Device Client β Monitor Client
β β β
ββββββββββββββββββββββββΌβββββββββββββββββββββββ
β
βββββββββββββββΌβββββββββββββββ
β β
β WebSocket Server β
β (Face Scanner API) β
β β
ββββββββββββββββββββββββββββββπ¦ Room Structure
global- Public events for all clientsschool:{schoolId}- School-specific eventsdevice:{schoolId}:{deviceId}- Device-specific eventsstudent:{schoolId}:{studentId}- Student-specific events (auto-joined)
π Migration from Password Auth
Use the interactive migration tool:
node migrate-to-api-keys.jsThis will:
- Analyze your existing clients
- Generate API key creation commands
- Create updated configuration files
- Provide step-by-step migration guide
π Configuration
Environment Variables (.env)
# Server Configuration
WS_URL=ws://localhost:8080/ws
# Authentication Tokens
STUDENT_TOKEN=your-token-here
DEVICE_TOKEN=your-token-here
MONITOR_TOKEN=your-token-here
# Client Information
STUDENT_ID=std12345
SCHOOL_ID=sch001
DEVICE_ID=device456
# Connection Settings
HEARTBEAT_INTERVAL=30000
RECONNECT=true
MAX_RECONNECT_ATTEMPTS=10π€ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
π License
MIT License - see LICENSE file for details.
π οΈ Built With
- Node.js - JavaScript runtime
- ws - WebSocket client implementation
- dotenv - Environment variable management
- Claude Code - AI-assisted development
π Support
- GitHub Issues - Bug reports and feature requests
- Documentation - Check the
/docsfolder - Examples - See
/examplesfor usage patterns
π― Roadmap
- TypeScript definitions
- React/Vue.js integration examples
- Metrics and monitoring dashboard
- Mobile SDK (React Native)
- Load balancing support
β Star this repo if it helps you!
Built with β€οΈ using Claude Code