今日已更新 305 条资讯 | 累计 19865 条内容
关于我们

标签:#websocket

找到 4 篇相关文章

AI 资讯

Building a Real Time Sports Scoring Engine with WebSockets and DynamoDB Streams

The Problem Sports scoring sounds simple. One team scores a point, the number goes up, everyone sees it. But when you build it as a web application that needs to work on courtside tablets, spectator phones, and wall mounted displays simultaneously, with voice commands and tap controls, the architecture becomes more interesting. The project was Scoring AI, a voice enabled match scoring application for sports courts. Players start a match, share a link, and control the scoreboard from any device. The backend handles real time state synchronization, optimistic locking, idempotent score updates, rate limiting, and WebSocket broadcasting. The team was small. Me and a coworker who handled the CI/CD side. We were at the same level, both full stack, and we designed the system together. He focused on the deployment pipeline and infrastructure automation. I focused on the application layer, the real time system, and the frontend. But the architecture decisions were shared. This article covers the technical decisions we made and how patterns from previous projects influenced them. Why DynamoDB for Live Matches The match scoring data is different from the business data around it. A match lasts about an hour, gets updated frequently, and needs to be read by many viewers at once. After the match is complete, it is archived and rarely accessed. I had seen what happens when you put high frequency state updates into a relational database on a previous project. Row locks, contention, connection pool exhaustion. For Scoring AI, we used DynamoDB for the live match state and PostgreSQL for everything else. The hot path needed fast writes, optimistic locking, and automatic cleanup of abandoned matches. DynamoDB provides all of these. The version field on each match record acts as an optimistic lock. Every score update is a conditional write that checks the version has not changed. The cold path uses PostgreSQL through Kysely for user profiles, subscriptions, pricing plans, payment histor

2026-07-15 原文 →
AI 资讯

Deploying a real-time multiplayer game on Railway

This post contains Railway referral links. If you sign up through one I get a bit of credit. I build Old Light , a real-time strategy game that runs in the browser. Claim stars, grow an economy, send fleets, all while other players and NPC empires do the same. The second a build finishes or a fleet lands, the server pushes it to every connected client over a WebSocket. That last part, a long-lived server holding an open socket, rules out most of the usual hosts. Here's what it ruled in. Why not Vercel or Netlify Serverless shines when your backend is stateless functions. It's the wrong shape the moment you need a socket that stays open: socket.io wants one process that lives for the whole session, and serverless boots per request and then freezes. You can bolt on a managed WebSocket service, but that's a second system to run and pay for. Railway runs your service as a normal long-lived process, so socket.io just connects. Fly.io does this too with more knobs to turn. I wanted to ship, so Railway won. Monorepo, two services Old Light is an npm workspaces monorepo: a shared types package, an Express plus TypeORM plus socket.io API, and a Vite web app served by a small Express server. On Railway that's two services on the same repo, each with its own root directory and build command, shared built first. They deploy as separate origins, so the web app reads the API's URL from VITE_API_URL . Vite bakes that in at build time, so it's a build variable, not a runtime one. Postgres is a plugin that injects DATABASE_URL , and production runs migrations rather than synchronize . WebSockets need nothing special until you run more than one instance, at which point you'd add a Redis socket.io adapter. I haven't left a single box yet. A healthcheck that stops version skew Two services don't go live at the same instant. Push a commit that touches both, the web finishes first, and for a minute your new frontend is calling API routes that don't exist yet. It 404s, then heals itself o

2026-07-09 原文 →
开发者

WebSocket Reconnection That Actually Works: Auto-Reconnect Guide for Trading Bots

This post was originally published on MatrixTrak.com — the production reliability toolkit for trading bot operators and .NET engineers. Complete WebSocket auto-reconnect guide for trading bots. Implement automatic reconnection with exponential backoff, heartbeat ping-pong, message gap detection, and state recovery. WebSocket connections drop. Not maybe. Definitely. Exchanges reset connections every 24 hours. Networks glitch. Load balancers rotate. HTTP proxies timeout. Your trading bot will experience disconnects. The question isn't whether you'll disconnect—it's whether your bot recovers correctly when you do. If you only do three things Implement automatic reconnection with exponential backoff and jitter. Track sequence numbers to detect missed messages. Always verify state via REST after reconnect. Never trust WebSocket alone. WebSocket Auto-Reconnect Quick Start If you just need a working auto-reconnect loop right now, here's the minimum viable implementation: class AutoReconnectWebSocket { private ws : WebSocket | null = null ; private reconnectAttempts = 0 ; private maxRetries = 10 ; private shouldReconnect = true ; async connect ( url : string ): Promise < void > { return new Promise (( resolve , reject ) => { this . ws = new WebSocket ( url ); this . ws . onopen = () => { this . reconnectAttempts = 0 ; resolve (); }; this . ws . onclose = ( event ) => { if ( this . shouldReconnect ) { const delay = this . backoff (); console . log ( `[AutoReconnect] Closed ${ event . code } . Reconnecting in ${ delay } ms` ); setTimeout (() => this . connect ( url ), delay ); } }; this . ws . onerror = () => { /* onclose fires next */ }; }); } private backoff (): number { const base = 1000 ; const max = 30000 ; const delay = Math . min ( base * Math . pow ( 2 , this . reconnectAttempts ++ ), max ); return delay + delay * 0.2 * ( Math . random () * 2 - 1 ); // +20% jitter } close (): void { this . shouldReconnect = false ; this . ws ?. close (); } } This handles the core auto

2026-07-06 原文 →
AI 资讯

Why crypto arbitrage windows close before your REST poll completes

TL;DR : Crypto arbitrage windows on liquid pairs now close in under 100 ms. A REST polling loop typically takes 1–1.5 seconds round-trip. WebSocket delivers the same data in 20–100 ms. If you're still polling REST endpoints for orderbook data in 2026, you're missing the majority of opportunities — not because your strategy is wrong, but because your data plane is fundamentally too slow. This post walks through the math, shows a benchmark I ran on a handful of major exchanges, and provides production-grade Python code for a WebSocket client that handles reconnects, heartbeats, and orderbook reconstruction. 1. The numbers that broke REST polling When I started writing crypto arbitrage bots a few years ago, polling Binance's REST API every 500 ms was perfectly acceptable. Spreads were wide, arbitrage windows lasted multiple seconds, and the orderbook for BTCUSDT moved slowly enough that a half-second-old snapshot was still tradeable. In 2026, the same approach doesn't work. Here are the numbers as they stand today: Metric Value Median crypto arbitrage window on liquid pairs 30–80 ms Window closes in under 100 ms ~90% of cases REST round-trip latency (request → response → JSON parse) 1.0–1.5 seconds WebSocket update delivery latency (push from exchange to client) 20–100 ms The math is brutal. A 100 ms window cannot be caught by a 1500 ms poll. By the time your REST response arrives, the orderbook you're reading is 15 cycles stale. You're not "slow" — you're not even in the same temporal universe as the event you're trying to react to. 2. Why REST is fundamentally slow REST APIs over HTTPS carry overhead that adds up: TCP handshake — three packets to establish, typically 50–150 ms on intercontinental hops. TLS handshake — another full round-trip, 30–100 ms. HTTP request/response — the actual data exchange. JSON parse — depending on payload size, 5–50 ms. Rate-limit budget — most exchanges cap REST to 10–20 requests per second per IP. Polling faster gets you banned. Yes,

2026-06-02 原文 →