Ethereum: How to use incoming Binance WebSocket data?
Using Binance Websocket data with Ethereum
As a developer, you probably want to integrate real time market data into your programs. One popular solution is to use the Webocket Binance API to obtain real incoming data. In this article, we will investigate how to use Binance Websocket Binance data to track Ethereum prices.
Conditions necessary
Before you dive, make sure you have:
- Binantic account and valid API key.
2.
- Basic knowledge of Javascript and Websocket API.
The first actions with Binance Websockets API
To use API Binance Websockets, you will need:
- Create API key on the Binance website.
- Get Websocket URL by following the Binance documentation instructions.
For Ethereum prices, we will use ETH-APIS package, which provides a simple and convenient way to communicate with the API online Ethereum Blockchain.
Installation
To get started, install the required packages:
`Bash
NPM Binance-JS ETH-UNTIL
Websocket Link Setting
Here’s an example of how to get a link to Binance Websocket’s final point:
`Javascript
CONST {Web3} = Requirement (‘Web3’);
CONST binance = requirement (‘binance-js’);
CONST APikey = ‘your_api_key’;
CONST APULET = ‘Your_api_Secret’;
CONST websocketturl = ‘wss: //apis.binance.com/1/websocket’;
Const Web3 = New Web3 (New Web3.Providers.Httpprovider (WebSocketturl));
CONST ETHAPI = New Binans ({{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
Apikey,
Apisecret,
});
// Example: Look at the prices of Ethereum characters
Ethapi.get (‘Ethusdprice’, feature (error, answer) {
IF (ERR) console.error (ERR); // Change the blog of your choice
yet {{{
CONST PRICE = Response.price;
Console.log (Current Ethh Price: $ {Price}
);
}
});
`
In this example, we create a copy of the Eth-Apis package and pass our API key. We then join the Binance Websocket final point using the Web3 supplier.
Animal receiving messages
The input data is sent as JSON messages via Websockets. To analyze these messages, you will need to use the library such as Json-Stringify-Safe. Here’s an example:
`Javascript
CONST {Parsemessage} = Requirement (‘Json-Stringify-Safe’);
// Example: An animal’s first Binance message (in this case price update)
Ethapi.get (‘Ethusdprice’, feature (error, answer) {
IF (ERR) console.error (ERR); // Change the blog of your choice
yet {{{
CONST DATA = PARSEMESSAGE;
CONST {symbol, time mark, price} = data;
Console.log (received ETH price update $ {times}: $$ {Price}
);
}
});
`
In this example, we use Parsemessage to analyze the incoming message from Binance.
Integration into your program
To integrate our Websocket connection into your program, you will have:
- Create socket events for new Binance messages.
- Work with received messages and update your data accordingly.
Here is an example of how we can create a simple Websocket server using WS:
`Javascript
Const Websocket = Requirement (‘ws’);
// Create a socket connection with the Binance Websocket outcome
CONST WSS = New Websocket.server ({port: 8080});
wss.on (‘connector’, (ws) => {{{
Console.log (‘related client …’);
// Processing of incoming messages from Binance
ws.on (‘message’, (message) => {
CONST DATA = PARSEMESSAGE (Message);
If (Data && Data.Symbol == ‘eth’) {
Console.log (“ETH price update: $ {Data.price});
} ELSE {
Console.log (ignoring an unknown symbol: $ {Data.Symbol}`);
}
});
// Close the connection by turning off
ws.on (‘close’, () => {
Console.log (‘client off …
Leave a Reply
Want to join the discussion?Feel free to contribute!