Save Websockets data to the database using Ethereum
The API of Ethereum WebSockets API allows for two -way communication between client and server in real time. Data received from binance flow to a database such as mysql or postgresql.
assumptions
- Knowledge of JavaScript, Node.JS and Ethereum development
- Set the Ethereum base node (eg Ethereum Classic or Polygon) and Blockchain Explorer (eg ETERSCAN)
- Install the desired libraries:
Ws
,MySql2
andDOPENV '
Step 1: Create WebSocket connection
. You can use "WSS: //stream.binance.com: 9443/WS/BTCUSDT@Trade" to connect to Bitcoin USDT Trade Stream.
Javascript
Website Const = Require (‘WS’);
Const WSS = New Web.Server ({port: 9443, Secured: TRUE});
`
Step 2: WebSocket messages
If the message is received from a binancourse, you must handle it accordingly. You can use the Library as “WS” to analyze and process WebSocket messages.
`Javascript
Ws.on (‘Connection’, (Ws) => {
Console.log (“Client Connected”);
Ws.on (‘Message’, (Message) => {
Const Data = Json.Parse (Message);
// process the data received here …
Ws.send (Json.stringify ({type: ‘Result’, data}));
});
Ws.on (‘Close’, () => {
Console.log (“Client disconnected”);
});
});
`
Step 3: Save the data in the database
Data for using your database. We will use MySQL2to connect to your MySQL or PostGRESQL database.
Javascript
Const MySql = Require (‘MySql’);
Const dbconfig = {
Host: ‘Your_host’,
User: “Your_user”,
Password: “YouPasword”,
Database: ‘Your_database’,
};
Const Connection = MySQL.CreateConnection (DBCONFIG);
Connection.connect ((ERR) => {
If (err) {
Console.error (‘Error connection:’, ERR);
Return;
}
Console.log (“Connected as ID” + connection.Threadid);
// Send data to the database here …
Connection.end ();
});
`
Assembly of all this
Here is a complete example that shows how to store websites data to MySQL database:
`Javascript
Const Express = requires (‘Express’);
Const App = Express ();
Const Bodypser = Require (“Body Parking”);
Const ws = require (‘Ws’);
Const MySql = Require (‘MySql2/Promise’);
Const dbconfig = {
Host: ‘Your_host’,
User: “Your_user”,
Password: “YouPasword”,
Database: ‘Your_database’,
};
// Create WebSocket connection
Const WSS = New Web.Server ({port: 9443, Secured: TRUE});
Ws.on (‘Connection’, (Ws) => {
Console.log (“Client Connected”);
// handle incoming messages from binance flow
Ws.on (‘Message’, (Message) => {
Const Data = Json.Parse (Message);
// process the data received here …
// Save data to the database
Savedatatatatabase (data);
});
Ws.on (‘Close’, () => {
Console.log (“Client disconnected”);
});
});
// function to process and store WebSocket messages in the database
Async
try {
Const query = ‘insert to website_data (id, time stamp, data) values (??,?,?)’;
Const [Result] = Await Connection.execute (Question, [
null,
New date ().
Json.stringify (data),
]);
Console.log (‘inserted into the database:’, the result);
} Catch (ERR) {
Console.error (‘Error inserted into the database:’, ERR);
}
}
App.use (bodyponser.json ());
App.Listen (3000, () => {
Console.