2

I'm new to NodeJS and found it very interesting. I'm not having much problems with building servers with NodeJS. But when I went to develop a web browser-client, I came to know about Socket.IO. But I'm very much confused about this. Can we develop web client applications without using Socket.IOS for NodeJS servers?

Is there support for websockets in NodeJS without Socket.IO?

Joe
  • 41,484
  • 20
  • 104
  • 125
Nachiketha
  • 21,705
  • 3
  • 24
  • 32

3 Answers3

3

This question is old, but for future references.

Socket.IO is famous, and for node stream https://github.com/substack/stream-handbook should be interesting to you.

I personally do this on my own project:

http://kenokabe.github.io/MarkdownLive/

I write stuff using Markdown and needed a streaming preview, so I created by myself. The preview screen is a Browser HTML page, and the HTML contents rendered and updated incrementally in streaming manner.

This technology is generally called RPC(remote procedure call) . Socket.IO is familiar to many people and you can find abundant resource on the web, so good thing to start.

However, I personally do not use it anymore since it's a huge library just to do RPC between node and browser, especially when you do know you don't need any fallbacks to other than websocket.

The more concise and sophisticated (I think) and seamless way to do RPC here is to use

dnode or rpc-stream with Browserify.

Proof: the below is the code on the client side (browser) of my own project:http://kenokabe.github.io/MarkdownLive/

https://github.com/kenokabe/MarkdownLive/blob/master/www/js/index.js

(function() //-----
  {
    "use strict";

    $(document)
      .ready(function()
      { //--------------------------
        $('#streamDIV')
          .html('<h1>MarkdownLive</h1><h3>Markdown Streaming Live View for SublimeText3</h3><br><h4><strong>Open</strong> <br><br>.md<br>.markdown<br>.mdown<br>.mkdn<br>.mkd<br>.mdwn<br>.mdtxt<br>.mdtext<br>.text<br>.txt</h4>');
        var through = require('through');

        var stream = require('shoe')('/stream')
          .pipe(through(function(data)
          {
            $('#streamDIV')
              .html(data);
          }));
        //-------------------------
      });

  }());

Using node.js stream properly, it's possible to write a server-client RPC code in concise Declarative programming or FRP manner.

There is no native implementation of webSocket in node.js Web Sockets server side implementation for NodeJS and Socket.IO is based on ws.

WebSocket layer

ws or sock-js or the others.

Stream layer

https://github.com/maxogden/websocket-stream for ws

https://github.com/NodeGuy/WebSocketStream for ws

https://github.com/kenokabe/WebSocketStreamPlus (my work based on NodeGuy's one)

https://github.com/substack/shoe for sock-js

RPC layer

https://github.com/substack/dnode

https://github.com/dominictarr/rpc-stream

Community
  • 1
  • 1
2

Yes, socket-io is a package that can be used with node-js (and other dependencies) but it is not mandatory when building web applications in node to use socket-io. Normal http requests will do just fine. You can also use web sockets in node without using socket-io.

The Internet
  • 7,959
  • 10
  • 54
  • 89
2

Sure, you can use it outside of socket.io. It's just one module available for node at npm.

For example, it's very common to use node as a web/http server - look at express. It's a web server and is very common and not necessarily a socket.io app.

Socket.io is good for scenarios where you want the browser/client to hold a socket with the server through html5 web sockets. In that scenario the server can call back to the client and pass data back (as opposed to polling) - calbacks based on some event on the server like another client calling and changing data. A chat application is the canonical example.

The reason you see socket.io mentioned so much along with node is because node is a network server that's very light weight and fast and therefore appropriate and optimized for close to real time network scenarios.

bryanmac
  • 38,941
  • 11
  • 91
  • 99