1

I have a react app and an android application. Now I send data from android to my server. Data is update in my mongoDB. After that I want to show it in my react app that a new data is added. Now I am not able to understand how to re-render my react app to show the users that data has been changed.

Possible solution I thought about:

  1. I create a async sleep to un-mount and mount my component and do a db query in componentdidmount()
  2. I am clueless

Doing a sync and refresh every minute or two doesn't feel like a good development skill.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Shobhit Tewari
  • 535
  • 5
  • 20
  • 2
    there are a few ways to go about this. 1. you can use a polling method, which is just setting up a `setTimeout` or `setInterval` script that checks your endpoint and pulls in new data, updating the local state for the component; or 2. use a WebSocket when the component is mounted, and simply update when the you receive data from that socket. – Jhecht Jan 28 '21 at 18:38
  • So in websocket, I create a another server for the real time other than my API one? Like this example here: https://medium.com/factory-mind/websocket-node-js-express-step-by-step-using-typescript-725114ad5fe4 – Shobhit Tewari Jan 28 '21 at 18:58
  • 1
    I will assume yes, I can't read the medium article and I'm not hard pressed to use their site. – Jhecht Jan 29 '21 at 00:08
  • Sir can you give me a reference or so from your vast knowledge. I was also looking at WebRTC. – Shobhit Tewari Jan 29 '21 at 06:58
  • The rough of it here is that depending on how your API is setup you can likely have an endpoint that is web-socket enabled, but i don't know a whole lot about your setup so it's hard to give more concrete advice. – Jhecht Jan 31 '21 at 09:57

0 Answers0