is websocket better or setInterval http request better?

esplover
Posts: 2
Joined: Tue Sep 21, 2021 4:51 pm

is websocket better or setInterval http request better?

Postby esplover » Tue Sep 21, 2021 4:57 pm

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..
Last edited by esplover on Wed Sep 22, 2021 2:41 am, edited 1 time in total.

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: is websocket better or setInterval http request better?

Postby ESP_Sprite » 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.

esplover
Posts: 2
Joined: Tue Sep 21, 2021 4:51 pm

Re: is websocket better or setInterval http request better?

Postby esplover » Wed Sep 22, 2021 2:40 am

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.

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: is websocket better or setInterval http request better?

Postby ESP_Sprite » Wed Sep 22, 2021 12:04 pm

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.

Who is online

Users browsing this forum: No registered users and 81 guests