WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time.
After making an intial HTTP request, the client makes a WS request, which remains open so that data can be pushed in either direction.
The advantage here is that you can send data in either direction without having to include an entire HTTP request every time. No head and cookies because those can start adding up if you're sending a lot of requests back and forth.
WebSockets are a great tool for any app that needs realtime data, such as a chat or a stock ticker.
Open index.html
in this repo, then open Chrome DevTools and navigate to the Network tab. This repo contains a simple implementation of websockets.
Get a better look at an implementation of WS with this app that uses Rails 5's Action Cable: Chatty.
There's a tutorial for building this here.