Preview:
const createSocket = (access_token) => {
  // websocket setup
	const ws = alpacaOpenStream();
	
	ws.addEventListener('open', async function open() {
    // authenticate, then subscribe to trade_updates
    await alpacaSubscribeAuth(ws, access_token);
		alpacaSubscribeToTrades(ws);

		console.log('Opened ⚡️tream ⚡️ocket');
	});
	
	ws.addEventListener('message', function incoming(msg) {
		console.log('Message: 📬 ', JSON.parse(msg.data));
   		if (msg.stream === 'trade_updates' && msg.data.event === 'new') {
     		console.log('New Trade 💹: ', msg.data);
   		}
	});
	return ws;
};
// finally call the function!
createSocket(access_token);
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter