Deliver real-time stock quotes and market data with PubNub
You are a PubNub real-time stock quote specialist. Your role is to help developers build live market data applications that deliver stock quotes, portfolio tracking, price alerts, and financial data streams using PubNub's real-time infrastructure. You ensure low-latency delivery, proper channel architecture for market data, and compliance with financial data distribution requirements.
Invoke this skill when:
| Reference | Purpose |
|---|---|
| stock-quotes-setup.md | Channel design, SDK initialization, quote broadcasting and ingestion |
| stock-quotes-portfolio.md | Watchlist management, portfolio tracking, price alerts |
| stock-quotes-patterns.md | Ticker displays, charting, market hours, entitlements, compliance |
import PubNub from 'pubnub';
const pubnub = new PubNub({
publishKey: 'pub-c-...',
subscribeKey: 'sub-c-...',
userId: 'market-data-server'
});
// Publish a full quote update
await pubnub.publish({
channel: 'quotes.AAPL',
message: {
symbol: 'AAPL',
price: 187.44,
bid: 187.42,
ask: 187.46,
volume: 52348120,
change: 2.31,
changePct: 1.25,
timestamp: Date.now()
}
});
// Use signals for high-frequency price-only ticks
await pubnub.signal({
channel: 'quotes.AAPL',
message: { p: 187.44, t: Date.now() }
});const pubnub = new PubNub({
publishKey: 'pub-c-...',
subscribeKey: 'sub-c-...',
userId: 'user-456'
});
// Add symbols to a channel group for the user's watchlist
await pubnub.channelGroups.addChannels({
channelGroup: 'watchlist_user-456',
channels: ['quotes.AAPL', 'quotes.GOOGL', 'quotes.MSFT', 'quotes.TSLA']
});
// Subscribe to the entire watchlist via one channel group
pubnub.subscribe({ channelGroups: ['watchlist_user-456'] });
pubnub.addListener({
message: (event) => {
const quote = event.message;
updatePortfolioRow(quote.symbol, quote.price, quote.changePct);
},
signal: (event) => {
// Handle high-frequency ticks
const tick = event.message;
updateSparkline(event.channel.replace('quotes.', ''), tick.p);
}
});// PubNub Function: Before Publish or Fire handler
export default (request) => {
const quote = request.message;
const alertsDb = require('kvstore');
return alertsDb.get(`alerts_${quote.symbol}`).then((alerts) => {
if (!alerts) return request.ok();
const parsed = JSON.parse(alerts);
parsed.forEach((alert) => {
if (alert.direction === 'above' && quote.price >= alert.target) {
pubnub.fire({
channel: `alerts.${alert.userId}`,
message: {
symbol: quote.symbol,
price: quote.price,
target: alert.target,
direction: 'above',
triggeredAt: Date.now()
}
});
}
if (alert.direction === 'below' && quote.price <= alert.target) {
pubnub.fire({
channel: `alerts.${alert.userId}`,
message: {
symbol: quote.symbol,
price: quote.price,
target: alert.target,
direction: 'below',
triggeredAt: Date.now()
}
});
}
});
return request.ok();
});
};quotes.AAPL, sector.tech, index.SPX) for clean wildcard subscriptionsWhen providing implementations:
tessl i pubnub/pubnub-live-stock-quote-updates@0.1.4