There are lots of good overview sites to read about how websockets generally work like here and here.
In a nutshell, they initiate a connection with a certain type of HTTP request and then after that, they are a direct TCP bi-directional connection between client and server.
There is some server overhead to maintain an open socket to a client so if you were anticipating tens of thousands of these at once, you would have to make sure your server infrastructure was capable of that scale. The CPU load would only be proportional to how many sockets were busy at any given time as an idle socket doesn't take any CPU.
Is there a server cost to using WebSockets?
It really depends upon what you're comparing it to. webSockets are typically used when the server needs to be able to send data to a client when the data comes available (often called "server push"). The usual alternative to using a continuously connected webSocket is to have the client polling repeatedly, asking the server over and over if the server has anything new. If you compare a webSocket to repeated client polling, then the webSocket is generally way, way more efficient and you could scale your server a lot higher using webSockets than with frequently polling clients.
Servers can be properly configured to support hundreds of thousands of simultaneous (and mostly idle) webSocket connections such that the server scalability limit is limited by how much traffic you're sending to all these connected clients. If you're sending data to a client every few seconds and you have hundreds of thousands of connected clients, then that will take a lot of server horsepower (and bandwidth) to do that with any technology and webSockets would still likely be better than any competing technology. But, if webSocket connected clients are mostly just idle and data is just occasionally sent to them, then a webSocket implementation can scale hugely and efficiently.
Here are some other references on the topic:
Websockets and scalability
websocket vs rest API for real time data?
Websocket vs REST when sending data to server
Ajax vs Socket.io
Why to use websocket and what is the advantage of using it?
HTML5 WebSocket: A Quantum Leap in Scalability for the Web
Push notification | is websocket mandatory?
The Comet library attempts to support a WebSocket-like interface, even when there is no direct WebSocket support. This is where the somewhat inefficient hacks start showing up as it tries to simulate a bi-directional TCP socket by keeping an open HTTP connection. If you are using real WebSockets, this is not an issue.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…