Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to best implement socket client in Node.js and relay to frontend?

I'm thinking to implement some socket api from a crypto exchange (e.g. https://github.com/binance-exchange/node-binance-api) , but I see that most libraries provided are using websocket in Node.js.

Since the api would require to pass api key and secret, and that is not recommend to implement such package in the frontend. I wonder what is the best implementation in such setup?

I would like to use the websocket data (like live prices and candlestick data) in my frontend. Should I create another websocket server in Node.js, emit new messages when I receive new data from the exchange socket (relaying) and my frontend just connect as a websocket client to my Node.js server?

like image 401
LawrenceH Avatar asked Dec 07 '25 05:12

LawrenceH


1 Answers

Should I create another websocket server in Node.js, emit new messages when I receive new data from the exchange socket (relaying) and my frontend just connect as a websocket client to my Node.js server?

Yes. The only way to protect security credentials is to keep them in the server. So, you can have your node.js server use those security credentials to connect to the crypto exchange and then you can have your client connect to your node.js server and then whenever you get an update from the crypto exchange, your server can send that to the client over the client-connected webSocket or socket.io connection.

Since the api would require to pass api key and secret, and that is not recommend to implement such package in the frontend.

You are correct. The secret credentials need to stay on your server and your server can then relay information to the client when there is new info to share. This is a classic design. Do, the secure work on the server and relay data to the client.

like image 150
jfriend00 Avatar answered Dec 08 '25 19:12

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!