How It Works
The real-time system uses a simple event-based architecture:- A client makes a change (creates/updates/deletes a task or comment)
- The client emits a
database-changeevent to the server - The server broadcasts a
database-changedevent to all connected clients - Each client receives the event and refreshes its data
Server Configuration
The Socket.io server is integrated into the Vite development server via a custom plugin invite.config.js:
Environment Variables
The Socket.io server URL. Defaults to
http://localhost:3000 if not set.CORS origin for Socket.io connections. Defaults to
* (allow all) if not set.Client Setup
To connect to the PhotoFlow Socket.io server from your client application:Installation
Basic Connection
Events
Emitting Changes
When you make a change to the database via the API, emit adatabase-change event:
Event to emit when you make a database change. The message payload is flexible and can contain any data you want to pass to other clients.
Listening for Changes
Listen for thedatabase-changed event to be notified when other clients make changes:
Event emitted by the server when any client triggers a database change. Contains the same payload that was sent in the
database-change event.Complete Example
Here’s a complete example of integrating Socket.io with task management:Connection Events
Socket.io provides built-in connection events:Best Practices
Debounce refreshes: When receiving
database-changed events, consider debouncing your data refresh function to avoid excessive API calls if multiple changes occur rapidly.Include context in events: Pass meaningful data in your event payloads (like
type, taskId, timestamp) to help clients decide whether they need to refresh.Troubleshooting
Connection Fails
If the socket connection fails:- Verify
VITE_SOCKET_URLis set correctly - Check that the Vite dev server is running
- Verify CORS settings if connecting from a different origin
Events Not Received
If clients don’t receive updates:- Ensure you’re calling
socket.emit('database-change')after API calls - Check the browser console for socket connection errors
- Verify the event name is exactly
database-change(notdatabase-changed)