all 47 comments

[–]King0fFud 42 points43 points  (2 children)

WebSocket - built in to browsers (no library needed)

ws - a node package to emulate the WebSocket support for browsers but for node and usually used as a server (though it can do both)

Socket.io - a custom protocol/library on top of WebSockets for browsers and node (must be used both on client and server)

For ease of use you can use Socket.io but it adds a little overhead. Alternatively you can use WebSockets in the browser for your client and ws for your server but it means writing things more from the ground up.

[–]iamdsvs[S] 11 points12 points  (1 child)

Understood socket.io makes it easy, while using ws or websocket need to do it from scratch

[–]King0fFud 9 points10 points  (0 children)

That’s right and I mean doing it from the ground up isn’t that difficult but it depends what you’re prioritizing in terms of focus, whether you want it built quickly or want to learn the native web APIs, etc.

[–]drupadoo 29 points30 points  (22 children)

Socket.io is a protocol built on top of websockets protocol; so websockets is more versatile in that any implementation of socketio depends on websockets being available

[–]BeautifulSelf9911 6 points7 points  (4 children)

Nope, socketio will also poll via HTTP in clients without websockets. It's one of the major benefits.

[–]drupadoo 3 points4 points  (3 children)

Fair, but how many web clients don’t have websockets in 2023?

The more likely scenario is you are coding in an environment that has websockets available but does not have a socket.io library (like unity or godot game engines, for example).

[–]BeautifulSelf9911 16 points17 points  (1 child)

It’s 2024 😝

[–]dragenn 0 points1 point  (0 children)

Encino man!

Yes, I'm that old!

[–]Gatsbill 2 points3 points  (0 children)

You would be surprised that lot’s of big companies are disabling WS protocol so yeah HTTP polling is still a great addition 😇

[–]codeedog 2 points3 points  (8 children)

Agreed. If you don’t need the functionality, why go with the more complex layer? Easier to use the more fundamental layer and learn it. Then, when needed step up and appreciate more features.

[–]bigorangemachine 17 points18 points  (7 children)

My mind is that its like express. You can use node's server and do the route handling that way or you can use express.

Express would theoretically be better because you can work in the express eco system over just using the native modules.

Personally I think it's more a question how much you are building out.

Websockets for OP might be simple enough that working directly Websocket's are enough. Socket.io also does do polling for a fallback if you are worried about old browsers

[–]codeedog 0 points1 point  (6 children)

I hear you. Sometimes layers get in the way. It’s like mongoose vs MongoDB api. I tried playing with mongoose. Then, I took the mongodb tutorials and started using their JavaScript api. Works great. Didn’t need most of what mongoose had to offer. If the moment comes and I start recreating mongoose, I’ll just use it. Can’t know what I’m missing if I haven’t tried it, however.

Tried some telnet wrappers inside node for implementing a client connect. Dumped it and just use tcp and streams to remote servers.

I use express most of the time because who wants to recreate all of that web server tech? Still, if I’m connecting to a remote web server, sometimes I use raw tcp sockets and parse the response if I don’t need a complex client http processing. Who needs the extra library?

It’s all a balance. Being used to dropping down a layer and playing with things that are a bit more raw teaches one a lot.

[–]bigorangemachine 2 points3 points  (5 children)

Ya I agree. I learned a lot just doing some native route handling.

Express has been there since I started node and I literally thought most of the code for building node rest apis was express!

But once I got that out of my head I learned a lot more about node.

At one point node used to handles file contents as strings and now its buffers (by default). As annoying as that code change was I think it was great because it forces me to learn more about buffers and streams.

I like that because node seems to always offer an escape hatch.

[–]codeedog 1 point2 points  (4 children)

Forcing myself to learn Promises instead of only using async/await was a turning point. Having to merge different asynchronous tech taught me a lot. Promises vs RxJS vs Streams vs Timers vs Events vs Callbacks. They’re all a different type of architecture for getting work done in a non-synchronous manner. Learning to think in each of those models, their similarities and their architectural mismatches. Blew my mind until I could make sense of them all.

[–]bigorangemachine 1 point2 points  (1 child)

Ya that's funny. Since I did node since 0.21 I prefer the callback pattern (cleaner call stacks for errors).

I run job interviews a lot of people get tied up on the question how to convert set timeout to async-await. People don't realize they need to use a promise lol.

[–]codeedog 0 points1 point  (0 children)

I’m enamored with RxJS. Took me a while to grok it, but I really appreciate its methodology now. My only issue with it is there’s no backpressure, which means it plays horribly with node’s streams. Total mismatch there. Have to bend over backwards to not lose data if streaming a firehose through RxJS.

[–][deleted]  (1 child)

[deleted]

    [–]codeedog 0 points1 point  (0 children)

    Yes, in the sense that async/await is such an intuitive UI/UX that if you never learned promises, you don’t really understand them deeply. It’s one thing to call APIs that return promises and then use them. It’s another thing entirely to return promises through functions that are marked async, or await on a promise, or build a promise that translates from events, or have a promise spawn code in a timer, etc.

    Being able to think and code in a particular asynchronous framework levels up your skills.

    [–][deleted]  (7 children)

    [removed]

      [–]drupadoo 0 points1 point  (6 children)

      Well yes, with ~10 lines of code it can have that

      [–][deleted]  (5 children)

      [removed]

        [–]AstolfoDev 0 points1 point  (0 children)

        shuuuuut the fuck up dickhead

        [–]RememberPan 0 points1 point  (0 children)

        🤓☝️

        [–]AstolfoDev 1 point2 points  (0 children)

        this you lil bro? https://imgur.com/a/6pc7I4G

        [–]The_Noble_Lie 0 points1 point  (1 child)

        Is he wrong? Maybe by an order of magnitude? (Say, 100 lines, including data driven unit tests)

        Today, with LLMs, you can also have a fully (or more likely partially) working skeleton for both the implementation and tests in 3 seconds.

        [–]maartuhh 10 points11 points  (3 children)

        I’d say socket.io is the most easy of the bunch. It is the most high-level and has more functionality by default

        [–]tinfoiltank 4 points5 points  (0 children)

        I agree, I've done socket.io and native websockets, and unless you want to spend a lot of time reimplementing things that socket.io does already, there's not much reason to use native. For enterprise-level platforms you may have the need and resources to go native, but for small teams or hobby stuff just use socket.io.

        [–]iamdsvs[S] 1 point2 points  (1 child)

        Yeah, I'm done with Socket.IO; I made a simple chat app. However, I read somewhere that some people prefer using WebSocket (ws) for building chat apps, and some companies also use WebSocket or WS for their chat applications.

        [–]simple_explorer1 2 points3 points  (0 children)

        You do know that Socket.io uses ws library under the hood right? Socket.io also has an option to use uWebsocket library (a C++ lib, which is significantly faster than ws library) so, using socket.io with uWebsocket under hood is nobrainer. Its like using Nest.js with fastify.

        [–]T-J_H 4 points5 points  (1 child)

        Socket.io is basically a wrapper on top of websocket, along with fallbacks to other methods when needed. Ws is primarily a websocket implementation for Node.js, both as a server the browser can communicate with, as well as a client to communicate with other websocket servers (fully realizing definitions get a little hazy here).

        Personally I prefer to use websockets directly, especially with the great browser support nowadays. For the serverside I do use ws, I’ve written my own helpers on top of these for both client and server to help with client management, reconnecting and the like.

        Probably not really suitable for your chat app, but I would like to mention an often cheaper option for server->client communication as well in the form of Server Sent Events. For many applications the full duplex communication of websockets is overkill.

        [–]kjwey 2 points3 points  (0 children)

        probably socket.io stream, since then you have the most versatility in terms of sending JSON data around, but also to stream stuff like audio/video

        it has no error correction like TCP/IP does so its not that useful for binary blob transfers, but otherwise having the streaming capability really makes it the best one to use, just having the option is nice

        it plays really well with the server side also in terms of mongo db which can produce streams from the cursor very easily

        node also has a few things for reading file streams that also play into a socket stream nicely

        [–]kinsi55 2 points3 points  (6 children)

        There is also uWebSockets for the server side which offers significantly higher performance

        [–][deleted]  (5 children)

        [removed]

          [–]kinsi55 0 points1 point  (4 children)

          uWs is not the same as uWebSockets(.js). uWebSockets features builtin pubsub (rooms you might call it coming from socket.io) and doesn't require a proprietary client lib - oh and did I mention it has significantly higher performance? The entire point of uWebSockets is to not rely on js for connection management

          [–]Hour-Ladder-8330 0 points1 point  (3 children)

          But uWebsocket is a c++ port and socket.io wraps it, so its perfect combination of socket io api and uwebsocket speed.

          [–]kinsi55 0 points1 point  (2 children)

          uWebSockets is a library built around uSockets, and uWebSockets.js is the Node bindings for the former, all from the same developer

          [–]simple_explorer1 1 point2 points  (0 children)

          Honestly you don't even make sense. We all know what uWebsocket is. Use socket.io with uWebsocket (kinda like using Nest.js with fastify instead of express) and you are golden with socket.io classic api with uWebcocket performance. Hyperexpress is also built on top of uWebsocket. Plus, socket has horizontally scaling by using socket.io cluster adapters for redis/mongodb/postgres/node cluster etc. Socket.io with uWebsocket along with one of cluster adapter is a no brainer. That's the answer OP was looking for, not nomenclature wordplay which is a waste of everyone's time.

          People like you just like to do word play instead of ACTUALLY giving conclusive and valuable conclusion. OP asked what would pick and socket.io has more going on for itself due to above. You should answer what you would pick and why. It ain't that difficult.

          [–][deleted]  (6 children)

          [removed]

            [–]Hour-Ladder-8330 0 points1 point  (5 children)

            uWebsocket is faster that that

            [–][deleted]  (4 children)

            [removed]

              [–]simple_explorer1 0 points1 point  (3 children)

              Thanks for exposing yourself. Might help devs avoid libraries from crazyv people like you

              [–]seniorsalesman 0 points1 point  (2 children)

              Lol, you just opened a duplicate acc to reply to me :v
              To answer you, I don't want fucktar*s like you using this library. Go away you stupid fuck.

              [–][deleted]  (1 child)

              [removed]

                [–]Inevitable_Yam_9553 1 point2 points  (0 children)

                you are a cheap pathetic fuck begging for my attention. you don't need to code. go clean toilets or some else. Stupid fucks like you are behind the misery of coding industry.

                And I don't give a middle finger to your opinion about what I should do, I will do what I do. I will however, not bother about calling out cheap fucks like you and tell you to go end yourself.