Skip to main content

Best Practices

1. WebSocket Connection

  • Always check readyState === WebSocket.OPEN
  • Check connection status before sending messages
  • Use auto-reconnect feature

2. Token Management

  • Store tokens in localStorage
  • Check token refresh time
  • Send token in every message

3. State Management

  • Update state only with gameState messages from server
  • Keep local state synchronized with server state

4. Event Handling

  • Clean up event listeners on component unmount
  • Keep event payloads type-safe

5. Error Handling

  • Catch all WebSocket errors
  • Show meaningful error messages to users
  • Add retry mechanism

6. Play Again Flow

  • Track the 30-second timeout
  • Show/hide modals at the right time
  • Don't forget to send message to parent window

Important Notes

  1. Token Refresh: Tokens are automatically refreshed. shouldRefreshToken check is performed.

  2. State Recovery: When connection is lost, state is recovered via state_recovery message.

  3. Action State: After each action, action state is reported via actionState field.

  4. Timer Management: Timers are managed by server. Client only displays them.

  5. Penalty System: Penalty system is managed by server and comes in gameState.

  6. Meld Management: Sets (melds) are validated by server and stored in openSeries field.

  7. Play Again Timeout: There is a 30-second timeout at game end. Play again request can be sent during this time.

  8. Parent Window Communication: window.parent.postMessage is used to return to lobby.


Last Updated: 2024