Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: echo.attach is not a function #304

Open
qiulang opened this issue Dec 26, 2022 · 2 comments
Open

TypeError: echo.attach is not a function #304

qiulang opened this issue Dec 26, 2022 · 2 comments

Comments

@qiulang
Copy link

qiulang commented Dec 26, 2022

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
Copy link

insanity54 commented Dec 27, 2022

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
Copy link
Author

qiulang commented Dec 28, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants