What to notice
This demo uses a small custom Next server because WebSockets need an HTTP upgrade, which is lower-level than an App Router route handler. Once the upgrade succeeds, the browser and server keep a shared socket open for two-way messaging.
In this example, the server sends a connected message once, a tick message every 2 seconds, and an echo message whenever you send text from the browser.