Developers
Caduceus WebSocket Endpoints
8min
websocket is a bidirectional communication protocol based on tcp that standardises communication between a client and a server, allowing for both parties to request data from one another in contrast, a unidirectional protocol like http only allows for the client to request data from the server the json rpc endpoint that you would use to connect to caduceus is an example of a unidirectional protocol using http a websocket connection between a client and a server can stay open as long as the parties wish it to maintain the connection, allowing for continuous communication with http, each connection begins when the client makes a request and ends the connection when the request is fulfilled websockets may be superior for web3 dapp notifications because they enable real time notifications for critical events continuously compared to requiring individual requests so if your d'app needs to monitor the chain and react to certain events then this is the right place to look for caduceus websocket endpoint for mainnet the websocket endpoint url is wss\ //mainnet ws caduceus foundation wss\ //mainnet ws caduceus foundation wss\ //mainnet ws caduceus foundation currently we do not support websockets for the galaxy testnet you can either embed this in your web3 provider like ethers js or use a cli tool like wscat and call it directly from your terminal let us show you how you do this using wscat on the terminal on mac first you will need to install wscat you can do so using npm npm i g wscat then you need to you can call \> wscat c "wss\ //mainnet ws caduceus foundation" \# this will open up a connection and it will show the following message if \# successfully connected connected (press ctrl+c to quit) \> now enter the json payload to indicate the event that you want to listen to a specific event \> wscat c "wss\ //mainnet ws caduceus foundation" \# this will open up a connection and it will show the following message if \# successfully connected connected (press ctrl+c to quit) \> { "id" 1, "method" "eth subscribe", "params" \["newheads"] } \# this will return a json payload, where the result 0x6daff287831915b5c075500ca0ba5ce5 is the subscription id < {"jsonrpc" "2 0","result" "0x6daff287831915b5c075500ca0ba5ce5","id" 1} \# you will now start to receive block data for every new block that is mined for more information about the eth specification then check out our eth subscribe documentation eth subscribe docid\ j23chqvfyfgkcyw6hus6k ethers js ethers provides a websocket provider so that you can listen to your scripts in your javascript or your nodejs application import { ethers } from 'ethers'; // the caduceus mainnet websocket url const url = 'wss\ //mainnet ws caduceus foundation'; // currently there is no galaxy testnet websocket url // this is a typescript script that can be run by doing `ts node \<insert file path here>` const init = function () { const customwsprovider = new ethers providers websocketprovider(url); console log('loading'); // listens to the latest blocks and prints the number of transactions for each block customwsprovider on('block', (res any, err any) => { console log('block', res); customwsprovider getblock(res) then(function (fullblock) { console log(`${fullblock transactions length} transactions in block ${res}`); }); }); // listens to the wrap cmp contract events const eventfilter = { address '0x1fcba3cb797465f38839f48ca7c9cda9d9aac28b', }; customwsprovider on(eventfilter, (res any, err any) => { console log('logs of wrap cmp', res); }); // logs any potential errors customwsprovider websocket on('error', async () => { console log(`unable to connect retrying in 3s `); settimeout(init, 3000); }); // reopens connection if the connection closes customwsprovider websocket on('close', async (code any) => { console log(`connection lost with code ${code}! attempting reconnect in 3s `); customwsprovider websocket terminate(); settimeout(init, 3000); }); }; init(); you can run this script by doing ts node /caduceus websockets ts you need to have ts node installed and point to the name of the file in our case we named our file caduceus websockets ts remeber to check the full documentation for eth subscribe here eth subscribe docid\ j23chqvfyfgkcyw6hus6k