importasyncioimportjsonimportosfromwebsocketsimportconnectNODE_URL=os.getenv('NODE_URL','https://reference.symboltest.net:3001')WS_URL=NODE_URL.replace('http','ws',1)+'/ws'print(f'Using node {NODE_URL}')asyncdefmain():asyncwithconnect(WS_URL)aswebsocket:# Connect to websocket endpointresponse=json.loads(awaitwebsocket.recv())uid=response['uid']print(f'Connected to {WS_URL} with uid {uid}')# Subscribe to block channelawaitwebsocket.send(json.dumps({'uid':uid,'subscribe':'block'}))print('Subscribed to block channel')# Subscribe to finalizedBlock channelawaitwebsocket.send(json.dumps({'uid':uid,'subscribe':'finalizedBlock'}))print('Subscribed to finalizedBlock channel')# Handle incoming messagestry:asyncforraw_messageinwebsocket:message=json.loads(raw_message)topic=message['topic']iftopic=='block':block=message['data']['block']block_meta=message['data']['meta']print(f'New block: height={int(block["height"]):,}'f' hash={block_meta["hash"][:16]}...')iftopic=='finalizedBlock':finalized=message['data']print(f'Finalized: height={int(finalized["height"]):,}'f' hash={finalized["hash"][:16]}...')# Unsubscribe on exitfinally:awaitwebsocket.send(json.dumps({'uid':uid,'unsubscribe':'block'}))awaitwebsocket.send(json.dumps({'uid':uid,'unsubscribe':'finalizedBlock'}))print('Unsubscribed from all channels')try:asyncio.run(main())exceptKeyboardInterrupt:passexceptExceptionaserror:print(error)
constNODE_URL=process.env.NODE_URL||'https://reference.symboltest.net:3001';constWS_URL=NODE_URL.replace('http','ws')+'/ws';console.log(`Using node ${NODE_URL}`);try{constwebsocket=newWebSocket(WS_URL);// Connect to websocket endpointconstuid=awaitnewPromise((resolve)=>{websocket.addEventListener('message',(event)=>{constmessage=JSON.parse(event.data);resolve(message.uid);},{once:true});});console.log(`Connected to ${WS_URL} with uid ${uid}`);// Subscribe to block channelwebsocket.send(JSON.stringify({uid,subscribe:'block'}));console.log('Subscribed to block channel');// Subscribe to finalizedBlock channelwebsocket.send(JSON.stringify({uid,subscribe:'finalizedBlock'}));console.log('Subscribed to finalizedBlock channel');// Handle incoming messageswebsocket.addEventListener('message',(event)=>{constmessage=JSON.parse(event.data);consttopic=message.topic;if(topic==='block'){constblock=message.data.block;constblockMeta=message.data.meta;console.log('New block:'+` height=${parseInt(block.height,10).toLocaleString()}`+` hash=${blockMeta.hash.substring(0,16)}...`);}if(topic==='finalizedBlock'){constfinalized=message.data;console.log('Finalized:'+' height='+`${parseInt(finalized.height,10).toLocaleString()}`+` hash=${finalized.hash.substring(0,16)}...`);}});// Unsubscribe on exitprocess.on('SIGINT',()=>{websocket.send(JSON.stringify({uid,unsubscribe:'block'}));websocket.send(JSON.stringify({uid,unsubscribe:'finalizedBlock'}));console.log('Unsubscribed from all channels');websocket.close();process.exit(0);});}catch(error){console.error(error);}
asyncwithconnect(WS_URL)aswebsocket:# Connect to websocket endpointresponse=json.loads(awaitwebsocket.recv())uid=response['uid']print(f'Connected to {WS_URL} with uid {uid}')
constwebsocket=newWebSocket(WS_URL);// Connect to websocket endpointconstuid=awaitnewPromise((resolve)=>{websocket.addEventListener('message',(event)=>{constmessage=JSON.parse(event.data);resolve(message.uid);},{once:true});});console.log(`Connected to ${WS_URL} with uid ${uid}`);
# Subscribe to block channelawaitwebsocket.send(json.dumps({'uid':uid,'subscribe':'block'}))print('Subscribed to block channel')# Subscribe to finalizedBlock channelawaitwebsocket.send(json.dumps({'uid':uid,'subscribe':'finalizedBlock'}))print('Subscribed to finalizedBlock channel')
// Subscribe to block channelwebsocket.send(JSON.stringify({uid,subscribe:'block'}));console.log('Subscribed to block channel');// Subscribe to finalizedBlock channelwebsocket.send(JSON.stringify({uid,subscribe:'finalizedBlock'}));console.log('Subscribed to finalizedBlock channel');
# Unsubscribe on exitfinally:awaitwebsocket.send(json.dumps({'uid':uid,'unsubscribe':'block'}))awaitwebsocket.send(json.dumps({'uid':uid,'unsubscribe':'finalizedBlock'}))print('Unsubscribed from all channels')
// Unsubscribe on exitprocess.on('SIGINT',()=>{websocket.send(JSON.stringify({uid,unsubscribe:'block'}));websocket.send(JSON.stringify({uid,unsubscribe:'finalizedBlock'}));console.log('Unsubscribed from all channels');websocket.close();process.exit(0);});
Using node https://reference.symboltest.net:3001
Connected to wss://reference.symboltest.net:3001/ws with uid 9AQEv+DFuCuddfrJlNh7ERf8Zlg=
Subscribed to block channel
Subscribed to finalizedBlock channel
New block: height=3,176,948 hash=EDD1ED4C92E29655...
New block: height=3,176,949 hash=18DBF7E75B2CC003...
New block: height=3,176,950 hash=A3409E1951E8FC8C...
Finalized: height=3,176,948 hash=EDD1ED4C92E29655...
New block: height=3,176,951 hash=A7D80AFD75C4C918...
New block: height=3,176,952 hash=027D939F6E9D0DAE...
Unsubscribed from all channels