WebSocket quotes on a website push ticks to the browser without constant HTTP polling. REST remains useful for candle history and snapshots; the socket is for last price and the current bar when the market moves more often than you are willing to hit an endpoint.
In a broker terminal and on a live showcase, the difference is visible immediately: polling once per second across dozens of tabs heats PHP and the proxy, while the client still sees steps. A socket subscribed only to visible symbols is cheaper and smoother if you do not subscribe to the whole world.
Flow outline
- Market data source: exchange, aggregator, or your normalizer.
- Bus or WebSocket proxy with authorization and limits.
- Client: table widget, ticker, or chart datafeed.
- Reconnect and "feed live / stale" status in the UI.
Full API: quotes REST/WS. Product feed: quotes. Chart: TradingView.
Subscriptions and symbols
The client sends subscribe with a list of ids. The server responds with frames such as {symbol, price, ts, ...}. Symbol names must match REST and the account area. A mismatch such as BTCUSDT versus BTC/USDT creates a "silent" socket: the connection exists, but updates never arrive under the expected key.
- Check handshake and the first frame after subscribe.
- Compare ts with server and browser clocks.
- Compare price with the REST snapshot for the same symbol.
- Break the network: reconnect should happen, not an endless spinner with no text.
Load and fans of "subscribe to everything"
Subscribing to 300 pairs from the home showcase kills both the channel and the user's phone. For a market table, a short REST snapshot is often enough; keep the socket for an open card, a terminal, and a ticker with a short list. Ticker: cryptocurrency ticker. Showcase: CoinMarket.
On the backend, limit subscriptions per connection and connections per IP. Without limits, one curious DevTools user can create a DoS cheaper than the market can.
BO and Forex terminal
For option expiration, the tick near close time is critical. For Forex, stream continuity under margin is critical. A socket-break indicator in the terminal is mandatory: the trader must know the price is not updating. Products: terminal, BO, Forex, white label.
Do not calculate a deal from one channel and draw the chart from another temporary WS channel; you will get price complaints. One normalizer -> one stream -> UI and matching/expiration logic.
Proxy, SSL, .loc
WSS behind nginx must pass Upgrade headers. A broken proxy looks like an immediate close after open. On a local stand, watch the scheme: an https page with ws:// is mixed content. A certificate for wss has the same class of problems as REST API certificates.
Do not leave domain whitelists and subscription tokens in public template repositories. Key rotation without frontend update means a mass feed outage after deployment.
Observability
Metrics: active connections, frames per second, average ts lag, upstream errors, and client reconnect frequency if you send telemetry. A stale alert for key symbols matters more than a pretty dashboard. When lag grows, first check upstream and normalizer CPU, not "chart CSS optimization".
Subscribe/unsubscribe logs with symbol names help catch typos after adding an instrument in the admin panel.
Acceptance checklist
Script or video: connect, tick, disconnect, reconnect, match with REST. Demo symbol list. Limits. WSS URL. Behavior under 100% CPU on the VDS, at least briefly in the runbook. Server panel: MicroPanel.
WebSocket does not replace candle history and does not remove the symbol dictionary. It reduces polling load and makes last price live, provided subscriptions are short, break status is honest, and the flow is shared with REST.
Frame format and compression
Keep frames short: symbol, price, ts, optionally bid, ask, volume. Do not send full coin metadata in every tick; REST directory data is for that. If bandwidth is tight, enable permessage-deflate carefully and check normalizer CPU: compressing tiny ticks may cost more than raw JSON.
Batching: you can buffer ticks for 50-100 ms and send a batch if the UI does not draw every microtick. For BO expiration, clarify whether maximum freshness is required at close time; then reduce or disable batching near expiration for those symbols.
WS authorization: a short-TTL token with REST refresh, or a cookie session on the same domain. Limit a public anonymous showcase socket by symbol count and frequency. Do not expose the private terminal socket on the same endpoint without auth.
Upstream failover
If you have several sources, document priority and switch conditions. During a switch, last price may jump; the UI should either smooth it or show a break marker. Log switchovers, otherwise "suddenly different price" is investigated as a trade-calculation bug.
Staging test: kill upstream, see stale, restore upstream, see live without reloading the page. Add this to acceptance together with client reconnect.
Client code without surprises
Use exponential reconnect backoff, a retry limit with clear text, and ping/pong if the proxy cuts idle connections. Do not open a second WebSocket on every chart timeframe change; change the subscription. In hidden tabs, reduce frequency or pause DOM updates to avoid a frame storm on return.
Test laptop sleep, Wi-Fi to LTE change, and corporate proxy. These are real trader conditions on phones.
Final criterion: one minute of ticks, disconnect, reconnect, REST match, limits, and status in the terminal and showcase UI. Related documents: quotes and API.
Socket maintenance
Watch connection growth after promotions. Raise limits deliberately, not to infinity. Test upstream failover quarterly. Version client SDKs and JS widgets: an old terminal cached on CDN must not break on a new frame format. When changing the format, keep the price field compatible for at least one release.
Document the maximum subscribe list length. A client sending 2000 symbols should receive a clear error, not a dropped connection.
FAQ
Websocket quotes on a website?
WebSocket is needed when REST polling can no longer keep up with the market.
How long does “WebSocket quotes on a website” take?
About 6 minutes to read. In practice it depends on your hosting and database setup.
Do I need a dedicated server?
For most scripts, shared hosting or a VDS with PHP and MySQL is enough. See the VDS section and PHP/MySQL requirements.
Tradingview library php integration?
See the related manual for this query. tradingview library php integration
Cryptocurrency rates script for your site?
See the related manual for this query. cryptocurrency rates script for your site