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.
ESP32 Persistent Server/Client communication
-
- Posts: 1708
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: ESP32 Persistent Server/Client communication
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.
Alternatively, server-sent events (SSE) may be what you want.
Re: ESP32 Persistent Server/Client communication
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.MicroController wrote: ↑Sun Jun 25, 2023 5:50 pmAfter 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.
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.
-
- Posts: 1708
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: ESP32 Persistent Server/Client communication
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.
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.
Who is online
Users browsing this forum: Majestic-12 [Bot] and 84 guests