Search code examples
rubywebsocketeventmachine

Debugging websocket 1006


I'm building a server based on ruby-websockets-chat-demo but have extended it to support one-to-one client/server communications across isolated channels. I have the base server working fine at this point, but I need to continue to develop and extend upon it.

My issue is that, anytime an error occurs within the server, a close with a 1006 is thrown to the client and it simply terminates without any clue as to what happened. This makes sense in production, I assume, but it causes havoc with development.

I've tried setting my own rescues and traps, but they seem to be just ignored. Using RubyMine, I can set breakpoints and walk through the code to find where it broke and then identify the issue, but that is a real pain.

I don't know what is trapping the errors, whether it is faye-websocket, eventmachine, or something else, really. Because it is caught and handled silently, it's very hard to discover.

Is there someway to disable this trap? Or, what is the best way to debug issues within a faye-websocket based client and server? Search as I might, I haven't been able to find that information. Probably just haven't yet hit upon the appropriate search criteria.


Solution

  • Went back to basic Ruby and inserted this code at the server's "on close" event:

    if @debug
     puts "MyserverBackend>> Close entered. Last error:#{$!.class}:#{$!.to_s};Module:#{$0};"
     [email protected] { |backtrace| puts backtrace }
     exit
    end
    

    This identifies where the failure is occurring and enhances debugging. For example, when I forced the error by issuing "xputs" instead of "puts" in the "on message" event, I now receive this clear information:

    MyserverBackend>> Close entered.  Last error:NoMethodError:undefined method `xputs' for #<Myserver::MyserverBackend:0x4c9b2e8>;Module:.../ruby/bin/rackup;Line:2;
    .../projects/myserver/middlewares/myserver_backend.rb:45:in `block (2 levels) in call'
    .../projects/myserver/middlewares/myserver_backend.rb:43:in `each'
    .../projects/myserver/middlewares/myserver_backend.rb:43:in `block in call'
    ...
    

    Before this, my client would just terminate with a 1006 and the server would give me no notice at all of what had happened. Hope this helps someone else debug those nasty, ubiquitous 1006 errors.

    ADDED OPTION: I found another option. I execute this method as the very first statement of each WS method such as open/message/close/error. It is related to EventMachine, running under the covers within faye-websocket. I found it here.

    def catch_error
      EventMachine.error_handler do |e|
        puts "Catch_error: #{caller[0]}: Last error:#{$!.class}:#{$!.to_s}; Module:#{$0}; Line:#{$.}; Error raised during event loop: Error:#{e.message}" #.red
        raise
      end
    end