Page 1 of 1

ESP32 Persistent Server/Client communication

Posted: Sun Jun 25, 2023 10:52 am
by PoincQre
Hi,

I'm in the process of creating a Server(ESP) Client(Webapp hosted in the VFS), and I would like that the client receives in the occurence of an event in the server side new data. With HTTP communication that is not reachable since the client should first send a request to get the response through the Handler. Hence thinking of Websockets, but that also seems to be implemented based on HTTP meaning that first the websocket should receive a message from the client to react to it and send to response.

How can I achieve a one-way communication where the ESP is always a sender (permanently) and the client is only a listener where it doesn't have to send any request to get the answer?

Thanks in advance.

Re: ESP32 Persistent Server/Client communication

Posted: Sun Jun 25, 2023 5:50 pm
by MicroController
After initiation by the client, websockets are bi-directional and either side can send data at any time.
Alternatively, server-sent events (SSE) may be what you want.

Re: ESP32 Persistent Server/Client communication

Posted: Tue Jul 11, 2023 2:30 pm
by PoincQre
MicroController wrote:
Sun Jun 25, 2023 5:50 pm
After initiation by the client, websockets are bi-directional and either side can send data at any time.
Alternatively, server-sent events (SSE) may be what you want.
Thanks for the answer. you mentionned that: websockets are bi-directional and either side can send data at any time. but from the Websocket server example of esp-idf the server side, seem not to be able to send data at anytime, since it first have to wait for a request from the client that triggers the http handlers, and then a response is send back, and that exactly what hinders my goal for now, I would like to only send events from the ESP32 to a websocket client without basically waiting for a request first from the client that triggers my handlers.

As for the SSE, I have indeed looked for some documentation or support on how to implement that on ESP-IDF, but I was unsuccessful. A more guidance there would be appreciated.

Thanks again.

Re: ESP32 Persistent Server/Client communication

Posted: Wed Jul 12, 2023 6:49 am
by MicroController
See https://esp32.com/viewtopic.php?t=24445#p87263

Upon connection/first request from the client you retrieve the connection's socket descriptor (httpd_req_to_sockfd(req)) and store it. After that, you use httpd_ws_send_frame_async(...) on the descriptor to send data outside the request handler.