ESP32 S3 - WebSocket
WebSocket enables real-time, two-way communication between a web browser and a device like the ESP32 S3 — without ever reloading the page. In this tutorial, we will learn what WebSocket is, why it is ideal for smooth ESP32 S3 control and monitoring, and how to set it up with a practical chat application example.
What you'll build:
- A WebSocket server running on the ESP32 S3.
- A web page served by the ESP32 S3 that connects to the WebSocket server.
- A chat interface that lets you type a message in the browser and send it to the ESP32 S3.
- Real-time message replies from the ESP32 S3 displayed in the browser without page reloads.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
What is ESP32 S3 Websocket?
WebSocket is a technology that lets a web browser communicate directly with a web server in real-time, keeping a persistent connection open so data can flow in both directions at any moment. Without WebSocket, the browser must repeatedly refresh or poll the server to receive updates, which is slow and inefficient. With WebSocket, the ESP32 S3 and the browser stay connected continuously, enabling instant data exchange without page reloads — exactly the kind of responsiveness needed for control and monitoring projects.
Key Specifications
- Protocol: Full-duplex TCP-based WebSocket (RFC 6455)
- Direction: Browser-to-ESP32 S3 and ESP32 S3-to-browser simultaneously
- Latency: Near-instant; no page reload required
- Common use cases: Online games, instant messaging, live dashboards, IoT control panels
Why we need WebSocket to smoothly control ESP32 S3?
Imagine controlling a remote-controlled device using a web interface on your phone. Without WebSocket, every direction or speed change requires a page refresh — like pressing a "reload" button before each command reaches the device. With WebSocket, you have a continuous, direct connection between the browser and the ESP32 S3, so commands arrive instantly with no delay caused by reloading.
In general, WebSocket allows you to:
- Send data from the web browser to ESP32 S3 without reloading the webpage.
- Send data from ESP32 S3 to the web browser without reloading the webpage.
This enables true two-way communication in real-time.
Benefits of WebSocket with ESP32 S3:
- Real-Time Control: WebSocket enables instant communication with the ESP32 S3, ensuring quick responses to commands for a seamless user experience.
- Persistent Connection: Maintain a continuous link without refreshing the control page, creating an always-ready line of communication for immediate instructions.
- Efficiency: Experience prompt responses without the need for constant page reloading, enhancing overall user enjoyment and efficiency.
Web Chat with ESP32 S3 via WebSocket
The webpage's content (HTML, CSS, JavaScript) are stored separately on an index.h file. So, we will have two code files on Arduino IDE:
- An .ino file that is ESP32 S3 code, which creates a web server and WebSocket server
- An .h file, which contains the webpage's content.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Connect the ESP32 S3 board to your PC via a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the right ESP32 S3 board (e.g. ESP32S3 Dev Module) and COM port.
- Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE.
- Search "DIYables ESP32 WebServer", then find the Web Server library created by DIYables.
- Click Install button to install the Web Server library.

- On Arduino IDE, create a new sketch and give it a name, for example, newbiely.com.ino.
- The following code implements the ESP32 S3 WebSocket server — it handles incoming browser connections, receives messages, and sends replies in real time. Copy it and open it with Arduino IDE.
- Modify the WiFi information (SSID and password) in the code to match your own network credentials.
- Create the index.h file in Arduino IDE by:
- Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
- Give the file's name index.h and click OK button.
- The following code defines the webpage served to the browser, including the chat UI and the JavaScript that manages the WebSocket connection. Copy it and paste it into index.h.
- Now you have the code in two files: newbiely.com.ino and index.h.
- Click Upload button on Arduino IDE to upload code to the ESP32 S3.
- Open the Serial Monitor and check the output. You should see readings similar to the following (timestamps are for reference):
- Take note of the IP address displayed, and enter this address into the address bar of a web browser on your smartphone or PC.
- You will see the webpage as below:
- Click the CONNECT button to connect the webpage to ESP32 S3 via WebSocket.
- Type some words and send them to ESP32 S3.
- You will see the response from ESP32 S3.
- Pro Tip: If you modify only the HTML content in index.h without touching newbiely.com.ino, Arduino IDE may not recompile the HTML. To force an update, make a trivial change in the .ino file (e.g., add a blank line or a comment) before uploading.
- If you modify the HTML content in the index.h and does not touch anything in newbiely.com.ino file, when you compile and upload code to ESP32 S3, Arduino IDE will not update the HTML content.
- To make Arduino IDE update the HTML content in this case, make a change in the newbiely.com.ino file (e.g. adding empty line, add a comment....)




※ NOTE THAT:
Line-by-line Code Explanation
The above ESP32 S3 code contains line-by-line explanation. Please read the comments in the code!
How the System Operates
The ESP32 S3 code functions by creating both a web server and a WebSocket server, allowing the browser and the board to maintain a live, bidirectional channel. The process unfolds as follows:
- When you input the ESP32 S3's IP address into a web browser, a request is made for the webpage (User Interface) from the ESP32 S3.
- The ESP32 S3's web server responds by transmitting the content of the webpage (HTML, CSS, JavaScript).
- Subsequently, your web browser displays the webpage.
- When you click the CONNECT button, the JavaScript code embedded in the webpage establishes a WebSocket connection to the WebSocket server on the ESP32 S3.
- With the WebSocket connection established, when you type something and click the SEND button, the JavaScript code sends that text to the ESP32 S3 through the WebSocket connection in the background.
- Upon receiving the message, the WebSocket server sends back a reply to the webpage.
Real-World Applications
WebSocket is the foundation for a wide range of interactive ESP32 S3 projects that demand low-latency, continuous communication between the board and a browser-based interface.
- Remote Robot Control: Send movement commands to a robot car from the browser and receive live sensor feedback without any page refresh.
- Live Sensor Dashboard: Stream temperature, humidity, or motion data from the ESP32 S3 to a browser chart that updates in real time.
- Smart Home Panel: Control lights, fans, and relays from a single web page with instant on/off feedback.
- Industrial Monitor: Display machine status, error alerts, and counters from factory equipment connected to the ESP32 S3.
- Multiplayer IoT Games: Coordinate multiple ESP32 S3 devices through a shared WebSocket server for synchronized interactive experiences.
Video Tutorial
Watch the step-by-step video walkthrough for this ESP32 S3 project below.
Challenges
Once you have the basic WebSocket chat working, try pushing it further with these extension challenges. Each one reinforces a different aspect of real-time web communication with the ESP32 S3.
- Beginner: Add a button on the webpage that sends a fixed command string (e.g., "LED_ON") to the ESP32 S3 and have the board echo it back with a confirmation message.
- Intermediate: Modify the ESP32 S3 sketch to broadcast a counter value to all connected WebSocket clients every second, and display it as a live number on the webpage.
- Advanced: Implement a password-protected WebSocket connection — the browser must send a secret token as the first message, and the ESP32 S3 closes the connection if the token is wrong.
You can learn other ESP32 S3 WebSocket examples below: