Cjmarkham
Cjmarkham

Reputation: 9691

SailsJS, Socket IO 101 (Switching Protocols) pending forever

I have been working on an application using SailsJS. Everything has been working fine until a couple of hours ago when I wasn't getting responses from socket.get(). I checked the network tab in chrome console and found that the last request is still pending.

Uri for request:

ws://localhost:1337/socket.io/1/websocket/FBcw-Q4H3PLFXcIlb0GB

Status code:

101 Switching Protocols

I have searched around and some people have been saying it is due to firewalls. So I disabled them but this didn't help. Some others have suggested defaulting to xhr polling so I did that but the same thing happens, a request to

http://localhost:1337/socket.io/1/xhr-polling/4_gAWYQJtKdXhDFEcxHe?t=1395536371612

just stays pending forever.

In response to the comment here is the request event:

socket.get('/session/create', {
            username: username,
            password: password
        }, function (response) {
                console.log(response) // null
            });

The response is never returned and is always null in the callback. I even tried sending a response straight away and that isn't returned either. Example below:

// Session controller
module.exports = {  

    create: function (req, res) {
             return res.json({foo: 'bar'}, 200);
        }
}

Upvotes: 2

Views: 4674

Answers (1)

Cjmarkham
Cjmarkham

Reputation: 9691

Sorry, I have done some fiddling and I have found the issue (probably a silly mistake on my part).

I have shortcut urls enabled which in SailsJS means a request to /foo/bar will map to the FooController.bar

This worked fine until I added a wildcard route like the below example:

get /:foo/:bar

This is what seems to have broken the request. Since I was sending a request to /session/create and relied on shortcut routes without specifying an explicit route it was interpreted by the :foo/:bar route.

Lesson learned: Disable shortcut URLs and use proper routing.

Upvotes: 3

Related Questions