Skip to content

TypeError: echo.attach is not a function #304

Open

Description

@qiulang

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.

Activity

  1. insanity54 commented on Dec 27, 2022

    @insanity54

    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()

  2. qiulang commented on Dec 28, 2022

    @qiulang
    Author

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions