Page 1 of 1

is websocket better or setInterval http request better?

Posted: Tue Sep 21, 2021 4:57 pm
by esplover
I am building a wifi PIR system. When the PIR is triggered it will send the positive value to a site served on the esp32 server.

I have thought of 2 ways for this to work


1) Use a websocket to constantly trasmit data of PIR status (HIGH or LOW)

2) Build the HTML page served by the esp32 server and use javascript setInterval to constantly send http request to esp32 webserver to check the PIR status.

I built both and tested both. They both work as intended function wise. However which way is better ? There are some factors that i cant test such as which consumes more power etc etc..

Re: is websocket better or setInterval http request better?

Posted: Wed Sep 22, 2021 12:54 am
by ESP_Sprite
What's your use case? Do you simply want to have a browser open looking at the webpage, or do you have further automation in mind? Any other things?

Generally, sending the status continuously is a bit wasteful... you can't really do anything else if you use Javascript to refresh a page, but if you use Websockets, you theoretically only need to send a new value when something changes.

Re: is websocket better or setInterval http request better?

Posted: Wed Sep 22, 2021 2:40 am
by esplover
ESP_Sprite wrote:
Wed Sep 22, 2021 12:54 am
What's your use case? Do you simply want to have a browser open looking at the webpage, or do you have further automation in mind? Any other things?

Generally, sending the status continuously is a bit wasteful... you can't really do anything else if you use Javascript to refresh a page, but if you use Websockets, you theoretically only need to send a new value when something changes.
PIR door camera.

I want to know when someone stands in front of my house door and have a camera that i can turn on and see who is it or whether its just a cat. lol

So i combined esp32CAM with PIR.

Re: is websocket better or setInterval http request better?

Posted: Wed Sep 22, 2021 12:04 pm
by ESP_Sprite
I'd use websckets as a communication mechanism then; that should also allow you to push the images at whatever speed the sensor can generate them.