sockjs / sockjs-node

WebSocket emulation - Node.js server
http://sockjs.org
MIT License
2.09k stars 309 forks source link

TypeError: echo.attach is not a function #304

Open qiulang opened 1 year ago

qiulang commented 1 year ago

I just use the sample code, "A simplified echo SockJS server" on readme, but I got the error message

echo.attach(server);
     ^

TypeError: echo.attach is not a function

But I see attach() does exist at https://github.com/sockjs/sockjs-node/blob/main/lib/server.js#L58 so I can't figure out why.

insanity54 commented 1 year ago

I'm also seeing this issue.

But I see attach() does exist at https://github.com/sockjs/sockjs-node/blob/main/lib/server.js#L58 so I can't figure out why.

That's true in the source, but it's not the case in the npm package. Here's the Server class in my node_modules/sockjs/lib/sockjs.js (0.3.24)


  Server = (function(superClass) {
    extend(Server, superClass);

    function Server(user_options) {
      this.options = {
        prefix: '',
        response_limit: 128 * 1024,
        websocket: true,
        faye_server_options: null,
        jsessionid: false,
        heartbeat_delay: 25000,
        disconnect_delay: 5000,
        log: function(severity, line) {
          return console.log(line);
        },
        sockjs_url: 'https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js'
      };
      if (user_options) {
        utils.objectExtend(this.options, user_options);
      }
    }

    Server.prototype.listener = function(handler_options) {
      var options;
      options = utils.objectExtend({}, this.options);
      if (handler_options) {
        utils.objectExtend(options, handler_options);
      }
      return new Listener(options, (function(_this) {
        return function() {
          return _this.emit.apply(_this, arguments);
        };
      })(this));
    };

    Server.prototype.installHandlers = function(http_server, handler_options) {
      var handler;
      handler = this.listener(handler_options).getHandler();
      utils.overshadowListeners(http_server, 'request', handler);
      utils.overshadowListeners(http_server, 'upgrade', handler);
      return true;
    };

    Server.prototype.middleware = function(handler_options) {
      var handler;
      handler = this.listener(handler_options).getHandler();
      handler.upgrade = handler;
      return handler;
    };

    return Server;

  })(events.EventEmitter);

There's a note in the changelog about this. Seems like the information there is wrong, because it says installHandlers was renamed to attach.

Maybe that was the plan at some point, but installHandlers is still present in the package. So I think the answer is to use installHandlers instead of attach, or pull the code from github for the latest version.

EDIT-- I see that the main branch contains pre-release code. The latest stable branch is https://github.com/sockjs/sockjs-node/tree/v0.3.19 which has examples refeerencing .installHandlers()

qiulang commented 1 year ago

@insanity54 hi it is indeed like you said. Thanks!

May I ask another question ? If it is not appropriate to ask here please let me know. I try sockjs because I want to see if there is any scenario where sockjs is better than socket.io, which I already use. And as you can image my first try didn't give me great impression.

So from your experience if there is any scenario/use case where sockjs is better than socket.io ?

I did google that but I can't find any result too.