javascriptnode.jswebsocket

Does node have built in support for Websockets?


I seem to be confused by conflicting sources, yesterday I was reading the node docs and was sure Node's 'net' and 'http' modules had web socket capabilities, but maybe I misunderstood the documentation because today an article said that node has no built in web socket support.

Can you create a node server that can handle web socket connections with just node and javascript, no external libraries?


Solution

  • Just a prefix as I'm seeing it come up in a lot of the comments.

    Node 21 (stable at 22) introduced support for a websocket client. If you want a server (which is what is mentioned in the question, if not the title), then you still need to use a library (or roll your own).

    https://nodejs.org/en/learn/getting-started/websocket#important-to-understand


    Node does not have native support for websockets like it does for http or tcp (net) connections.

    It's been discussed a few times, and rejected for various reasons of the last few years. The current discussion is going on here: https://github.com/nodejs/node/issues/19308

    You can, of course, implement a websocket server yourself using the native modules, but you'll need to do a lot of boilerplate work.

    To see both an example of what you'd need to do to implement your own websocket server using node and a good pre-built library you can use to work with websockets in node, I would reccomend taking a look at: https://github.com/websockets/ws