Skip to content

✨ Networking - Implement P2P Handshake & Messaging #3

@jordyfontoura

Description

@jordyfontoura

✨ Networking — Implement P2P Handshake & Messaging

Description

We need to implement the initial P2P networking layer for Multibonk.
This includes the ability for two game clients to:

  • Discover and establish a direct peer-to-peer connection (with NAT traversal if possible).
  • Exchange simple messages reliably (e.g., ping/pong).
  • Fallback to relay (TURN) if direct connection fails.

This is the foundation for the multiplayer system, later features such as state synchronization and input replication will depend on this.

Scope

  • Create a NetworkManager (or equivalent) module to handle P2P connections.
  • Support WebRTC DataChannels for initial implementation (abstracted for future replacement if needed).
  • Implement a handshake protocol:
    • Exchange peer IDs.
    • Confirm readiness before sending game data.
  • Basic messaging API:
    • sendMessage(type, payload) for outgoing messages.
    • onMessage(type, callback) for incoming messages.
  • Add minimal logging for debugging connections.

Acceptance Criteria

  • Two local game instances can connect directly (when network permits).
  • Connection attempt retries at least 3 times before failing.
  • If direct connection fails, a fallback relay (TURN) server can be used (configurable).
  • Successful handshake confirms both peers are ready (log confirmation).
  • Ability to send a simple ping message and receive a pong response.
  • Network errors are logged with clear messages (no silent failures).
  • Unit tests for handshake and message serialization/deserialization.

Definition of Done

  • Code merged into main behind a feature flag (if applicable).
  • README or docs/networking.md updated with connection flow and developer setup.
  • CI passes with new networking tests.

Additional Notes

  • Future tasks will build on this to add state replication and client prediction.
  • The relay server config can initially be a simple TURN server URL/env variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions