3.2 KiB
Here is a clear and concise explanation of the task for an AI agent to fix the described problem.
Objective: Fix Session State Synchronization for New Clients
Your task is to correct a bug in the application's real-time collaboration feature. Currently, when a new client joins an existing session, they see the application's default state, not the session's current, modified state. This creates a state desynchronization issue.
Problem Analysis
The client-side application is already architected to handle an initial state dump upon connection.
-
useWebSocket.ts: When a message withtype: 'welcome'is received, the hook is designed to take themessage.payload.stateobject and pass it to the application's main state management hook. -
useDrumMachine.ts: This hook has auseEffectthat listens for incoming messages. It contains a specificcase 'state':that correctly processes a full state object, updating the grid, bassline, tempo, steps, and other parameters.
The problem is not on the client side. The root cause is on the server-side WebSocket implementation.
The server is likely doing one of the following incorrect things:
-
It sends a
'welcome'message that does not contain the complete, current state of the session. -
It sends a
'welcome'message with an empty or defaultstateobject. -
There is a race condition where the server sends the
'welcome'message before it has finished retrieving the current session state from its data store.
Required Implementation
You must modify the server-side WebSocket logic to ensure state is synchronized correctly.
-
On a new WebSocket connection: When a client connects and provides a
sessionId, your server-side code must immediately perform the following steps. -
Retrieve the complete current state for that
sessionId. This state must include:-
The drum
grid -
The
bassLine -
The current
tempo -
The number of
steps -
The
mutesarray -
The
drumVolume -
The
bassVolume
-
-
Construct a
'welcome'message. This message must contain the full state object you just retrieved. The JSON payload sent to the newly connected client must follow this exact structure:JSON
{ "type": "welcome", "payload": { "clientId": "UNIQUE_CLIENT_ID_FOR_NEW_CLIENT", "state": { "grid": [/* current grid state */], "bassLine": [/* current bassline state */], "tempo": 135, // The current tempo "steps": 32, // The current number of steps "mutes": [false, true, ...], // The current mute states "drumVolume": 0.8, "bassVolume": 0.5 } } } -
Send this
'welcome'message to the newly connected client immediately upon their connection. This must be the very first message they receive, ensuring the UI is rendered with the correct state from the start.
This change will ensure that any client joining an in-progress session will see the exact same state as all other participants, resolving the synchronization bug.