Skip to content

feat: add ping latency indicator and heartbeat.#244

Merged
imxade merged 2 commits into
AOSSIE-Org:mainfrom
Rozerxshashank:feat/ping-latency-indicator
Mar 4, 2026
Merged

feat: add ping latency indicator and heartbeat.#244
imxade merged 2 commits into
AOSSIE-Org:mainfrom
Rozerxshashank:feat/ping-latency-indicator

Conversation

@Rozerxshashank
Copy link
Copy Markdown
Contributor

@Rozerxshashank Rozerxshashank commented Mar 4, 2026

Addressed Issues:

Fixes #83

Description

This PR implements a real-time connection latency indicator and heartbeat mechanism to help users diagnose network performance issues, as requested in #83.

Key Changes
Backend websocket.ts: Added a minimalist ping handler that echoes back the client's timestamp via a pong message.

Core ConnectionProvider.tsx: Implemented a 2-second heartbeat interval that calculates Round-Trip Time (RTT) and exposes it via the connection context.

UI ControlBar.tsx: Added a color-coded LatencyBadge to the bottom toolbar:
Green (<50ms): Excellent connection.
Yellow (50-150ms): Noticeable lag.
Red (>150ms): High latency/unstable connection.

Screenshots/Recordings:

Functional Verification

  • Please check off the behaviors verified with this change.

Screen Mirror

  • Screen MIrror works.

Authentication

  • Connection doesn't work without a valid token.

Basic Gestures

  • One-finger tap: Verified as Left Click.

  • Two-finger tap: Verified as Right Click.

  • Click and drag: Verified selection behavior.

  • Pinch to zoom: Verified zoom functionality (if applicable).

Modes & Settings

  • Cursor mode: Cursor moves smoothly and accurately.

  • Scroll mode: Page scrolls as expected.

  • Sensitivity: Verified changes in cursor speed/sensitivity settings.

  • Copy and Paste: Verified both Copy and Paste functionality.

  • Invert Scrolling: Verified scroll direction toggles correctly.

Advanced Input

  • Key combinations: Verified "hold" behavior for modifiers (e.g., Ctrl+C) and held keys are shown in buffer.

  • Keyboard input: Verified Space, Backspace, and Enter keys work correctly.

  • Glide typing: Verified path drawing and text output.

  • Voice input: Verified speech-to-text functionality for full sentences.

  • Backspace doesn't send the previous input.

Any other gesture or input behavior introduced:

  • New Gestures: Verified any other gesture or input behavior introduced in this PR.

Additional Notes:

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.

  • My code follows the project's code style and conventions

  • I have performed a self-review of my own code

  • I have commented my code, particularly in hard-to-understand areas

  • If applicable, I have made corresponding changes or additions to the documentation

  • If applicable, I have made corresponding changes or additions to tests

  • My changes generate no new warnings or errors

  • I have joined the and I will share a link to this PR with the project maintainers there

  • I have read the

  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.

  • Incase of UI change I've added a demo video.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a latency indicator badge in the navbar displaying real-time connection latency with color-coded status (green for <50ms, yellow for <150ms, red otherwise).
  • Refactor

    • Removed connection status indicator from the touchpad area; connection monitoring now relies on the new latency badge.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6e5a8ff2-96ae-4195-82a4-104a79db73de

📥 Commits

Reviewing files that changed from the base of the PR and between 4afca78 and 5894fc3.

📒 Files selected for processing (3)
  • src/components/Trackpad/TouchArea.tsx
  • src/routes/__root.tsx
  • src/routes/trackpad.tsx
💤 Files with no reviewable changes (1)
  • src/components/Trackpad/TouchArea.tsx

Walkthrough

The PR implements a heartbeat-based latency measurement system. The client sends ping messages every 2 seconds; the server echoes back pong messages with timestamps. The client calculates round-trip time and displays it via a color-coded LatencyBadge component. The status prop is removed from TouchArea as it's replaced by latency visualization.

Changes

Cohort / File(s) Summary
Latency Measurement Core
src/contexts/ConnectionProvider.tsx, src/server/websocket.ts
Added heartbeat mechanism: client sends pings every 2 seconds when connected, server echoes pong responses with timestamp, client computes RTT and exposes latency state. Cleanup on disconnect or unmount.
Latency Display
src/routes/__root.tsx
Added LatencyBadge component that displays latency with color-coded indicators: green (<50ms), yellow (<150ms), red (≥150ms). Integrated into Navbar controls.
Status Prop Removal
src/components/Trackpad/TouchArea.tsx, src/routes/trackpad.tsx
Removed status prop from TouchArea and eliminated status-dependent decorative UI. Updated useRemoteConnection hook to stop returning status field.

Sequence Diagram

sequenceDiagram
    participant Client as Client<br/>(React)
    participant WebSocket as WebSocket<br/>(Connection)
    participant Server as Server<br/>(ws handler)
    participant UI as UI<br/>(LatencyBadge)

    Note over Client: status = "connected"
    Client->>Client: Set up 2s heartbeat interval
    
    loop Every 2 seconds
        Client->>WebSocket: Send ping<br/>(type: "ping", timestamp: now)
        WebSocket->>Server: Receive ping
        Server->>Server: Extract timestamp
        Server->>WebSocket: Send pong<br/>(type: "pong", timestamp: echo)
        WebSocket->>Client: Receive pong
        Client->>Client: Calculate RTT<br/>(now - pong.timestamp)
        Client->>Client: Update latency state
        Client->>UI: Render LatencyBadge
        UI->>UI: Color code based<br/>on latency value
    end

    Note over Client: status changed or unmount
    Client->>Client: Clear interval
    Client->>Client: Unsubscribe pong listener
    Client->>Client: Reset latency = null
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

Typescript Lang

Poem

🐰 A ping-pong dance through the network wire,
Latency measured, connection on fire!
Green means swift, red means slow,
Users now see the network's true glow! 💚🟡❤️

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly summarizes the main change: adding a ping latency indicator and heartbeat mechanism to measure connection quality.
Description check ✅ Passed Description is comprehensive, addressing the linked issue, explaining key changes across backend/core/UI, includes a screenshot, and completes most functional verification checklist items.
Linked Issues check ✅ Passed All core requirements from issue #83 are met: backend ping handler [websocket.ts], frontend heartbeat with RTT calculation [ConnectionProvider.tsx], and color-coded latency display [LatencyBadge in __root.tsx].
Out of Scope Changes check ✅ Passed All changes are scoped to the ping/latency feature: latency plumbing, heartbeat mechanism, ping handler, and UI indicator. Removal of status prop from TouchArea is a refactoring aligned with the feature's simplification goals.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/components/Trackpad/ControlBar.tsx`:
- Around line 43-45: The hardcoded "ms" unit in the ControlBar render should be
moved into i18n so the UI string is localizable: replace the literal "ms" in
src/components/Trackpad/ControlBar.tsx (the span that renders {latency}ms) with
a lookup from the i18n resource (e.g., t('latency.unit') or similar) and add the
corresponding key/value to the locale files; ensure the component imports and
uses the existing translation hook (e.g., useTranslation or t) and that the
rendered text becomes `${latency}${t('latency.unit')}` (or the equivalent
translation interpolation pattern used in the codebase).
- Around line 35-39: The ternary in ControlBar.tsx that chooses the text color
uses `latency < 150`, which makes 150ms render red; change that comparison to
`latency <= 150` so the middle branch covers 50–150ms inclusive. Update the
conditional expression that returns "text-green-400" / "text-yellow-400" /
"text-red-400" (the latency color selection logic) to use `<= 150` for the
yellow case so only values >150ms become red.

In `@src/contexts/ConnectionProvider.tsx`:
- Around line 178-182: The pong handler in ConnectionProvider uses
subscribe("pong", ...) and casts msg to { timestamp?: number } then computes
setLatency(Date.now() - pong.timestamp) without validating timestamp, which can
produce NaN; update the callback to explicitly check the type of pong.timestamp
(e.g., typeof pong.timestamp === "number" && Number.isFinite(pong.timestamp"))
before calculating RTT and only call setLatency when valid, otherwise ignore or
set a fallback/clear value to avoid propagating NaN into the context/UI.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: da695c80-7bee-4804-9294-32ba6a8aec45

📥 Commits

Reviewing files that changed from the base of the PR and between 54842a8 and 4afca78.

📒 Files selected for processing (3)
  • src/components/Trackpad/ControlBar.tsx
  • src/contexts/ConnectionProvider.tsx
  • src/server/websocket.ts

Comment thread src/components/Trackpad/ControlBar.tsx Outdated
Comment thread src/components/Trackpad/ControlBar.tsx Outdated
Comment thread src/contexts/ConnectionProvider.tsx
@imxade imxade merged commit 57e64d3 into AOSSIE-Org:main Mar 4, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Add Connection Latency (Ping) Indicator and Heartbeat Mechanism

2 participants