Skip to content

feat: add WebSocket endpoint for real-time price updates#121

Merged
Smartdevs17 merged 3 commits intoSmartdevs17:mainfrom
olathedev:feat/issue-80-websocket-price-updates
Mar 24, 2026
Merged

feat: add WebSocket endpoint for real-time price updates#121
Smartdevs17 merged 3 commits intoSmartdevs17:mainfrom
olathedev:feat/issue-80-websocket-price-updates

Conversation

@olathedev
Copy link
Copy Markdown
Contributor

Summary

Closes #80

  • ws://host/api/ws/prices — WebSocket endpoint on the same port as the REST API (no extra server needed)
  • Subscribe/unsubscribe — clients send { type: "subscribe", assets: ["XLM","BTC"] } or ["*"] for all assets
  • Push-only updates — prices are only broadcast when the value changes; unchanged prices are suppressed
  • Instant cache — subscribers receive last known price immediately on connect, before the next poll cycle
  • Heartbeat — server pings every 30 s to detect and clean up stale connections
  • Configurable pollingWS_PRICE_UPDATE_INTERVAL_MS (default 30 s), falls back from ORACLE_API_URL to CoinGecko public API

Message protocol

// Client → Server
{ "type": "subscribe",   "assets": ["XLM", "BTC"] }  // or ["*"] for all
{ "type": "unsubscribe", "assets": ["XLM"] }
{ "type": "ping" }

// Server → Client
{ "type": "subscribed",    "assets": ["XLM", "BTC"] }
{ "type": "price_update",  "asset": "XLM", "price": 0.12, "timestamp": 1711234567 }
{ "type": "unsubscribed",  "assets": ["XLM"] }
{ "type": "pong" }
{ "type": "error",         "message": "..." }

New env vars

Variable Default Description
ORACLE_API_URL (empty) Oracle service base URL; falls back to CoinGecko
WS_PRICE_UPDATE_INTERVAL_MS 30000 Price poll interval
WS_HEARTBEAT_INTERVAL_MS 30000 Heartbeat ping interval

Test plan

  • Connect a WebSocket client to ws://localhost:3000/api/ws/prices
  • Send { "type": "subscribe", "assets": ["XLM"] } — should receive subscribed ack
  • Wait for next poll cycle — should receive price_update for XLM
  • Send { "type": "ping" } — should receive pong
  • Send { "type": "unsubscribe", "assets": ["XLM"] } — no further XLM updates

)

- New PriceWebSocketServer class at ws://host/api/ws/prices
- Clients subscribe to specific assets (or '*' for all) via JSON messages
- Prices polled every 30s from oracle API (ORACLE_API_URL) with CoinGecko fallback
- Price updates pushed only when value changes (no redundant broadcasts)
- Cached prices sent immediately on subscribe for instant feedback
- Heartbeat pings every 30s to detect and clean up dead connections
- HTTP server in index.ts upgraded from app.listen to http.createServer
  so WebSocket and REST share the same port
- New WS config: WS_PRICE_UPDATE_INTERVAL_MS, WS_HEARTBEAT_INTERVAL_MS,
  ORACLE_API_URL env vars
- WebSocket message types added to src/types/index.ts
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 24, 2026

@olathedev is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 24, 2026

@olathedev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Smartdevs17 Smartdevs17 merged commit 4d90824 into Smartdevs17:main Mar 24, 2026
4 of 8 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 WebSocket support for real-time price updates in API

2 participants