Ethereum: How to save websockets data to DB

Save Data Web sites in the database using Ethereum

Ethereum’s Websockets API allows real -time, two -road communication between the customer and the server. In this article, we will study how to save Websocket data from Binance flow to a database, such as mysql or postgresql.

Prerequisites

  • Developed knowledge of JavaScript, Node.Js and Ethereum

  • Set the Ethereum Basic Node (eg Ethereum Classic or Polygon) and Blockchain Explorer (eg Etherscan)

  • Install the necessary libraries: WS,mysql2 and dotenv

Step 1: Create a web boot connection

At first, you need to establish a relationship with Binnet. You can use WSS: //stream.binance.com: 9443/WS/BTCUSDT@Trade Final Point to connect to Bitcoin USDT trading.

JavaScript

Const Websocket = Request (‘WS’);

Const WSS = New WebSocket.Server ({port: 9443, safe: true});

`

Step 2: Manage Web Reports

When the message is received from Binance Creek, you will have to pass it accordingly. You can use a library such as “WS” for analysis and processing of Websocket.

`JavaScript

WSS.ON (‘Link’, (WS) => {

Console.log (‘Customer Connected’);

ws.on (‘message’, (message) => {

Const Data = JSON.Parsse (Message);

// Treat the data received here …

ws.send (json.stringify ({type: ‘result’, data}));

});

WS.ON (‘Close’, () => {{{{{{{{{{{{{{{{{{{{{{{

Console.log (‘Customer Off’);

});

});

`

Step 3: Save data in the database

You will need to use API that supports interaction with your database to save Websocket data in the database. We will use the Library “MySQL2” to connect to your MySQL or Postgresql database.

`JavaScript

Const MyQL = Search (‘MySQL’);

Const dbconfig = {

Host: ‘Your_host’,

User: ‘Your_user’,

Password: ‘Your_Password’,

Database: ‘Your_database’,

};

Const Connection = MySQL.CONNECTION (DBCONFIG);

Connection.connect ((ERR) => {

IF (ERR) {

Console.Error (‘Fault Combination:’, Error);

return;

ICE

Console.log (‘connected as ID’ + Connection.Threadid);

// Send data to the database here …

Connection.end ();

});

`

It’s all together

Here is a full example of showing how to save WebSocket data in the MySQL database:

`JavaScript

Const Express = Request (‘Express’);

Const app = Express ();

Const Bodyparser = Request (‘Body-Pars’);

Const WS = Request (‘WS’);

Const MyQL = Request (‘MySQL2/Promise’);

Const dbconfig = {

Host: ‘Your_host’,

User: ‘Your_user’,

Password: ‘Your_Password’,

Database: ‘Your_database’,

};

// Create a web pocket connection

Const WSS = New WebSocket.Server ({port: 9443, safe: true});

WSS.ON (‘Link’, (WS) => {

Console.log (‘Customer Connected’);

// Processing incoming messages from Binance Creek

ws.on (‘message’, (message) => {

Const Data = JSON.Parsse (message);

// Treat the data received here …

// Save data in the database

Savedatodatatabase (data);

});

WS.ON (‘Close’, () => {{{{{{{{{{{{{{{{{{{{{{{

Console.log (‘Customer Off’);

});

});

// Processing and storing a web message feature

Savedatodatabase (Data) {{{{{{{{

Try {

Const Query = ‘Insert Websocket_Data (ID, Time Tags, Data) values ​​(?,?,?)’;

Const [score] = wait for link.execute (Investigation, [

zero,

New date (). Toisostring (),

Json.stringy (data),

));

Console.log (‘placed in the database:’, result);

} Capture (ERR) {

Console.Error (‘Error Inserting in the Database:’, ERR);

ICE

ICE

app.use (bodyparaser.jon ());

App.listen (3000, () => {

Console.

SOLANA TEST QUIC WIRESHARK

Leave a Comment

Your email address will not be published. Required fields are marked *

Enquire Now
close slider