Sails: socket.io.js - Failed to parse incoming socket.io request

Created on 11 Feb 2015  路  21Comments  路  Source: balderdashy/sails

Just tried to upgrade to sails 0.11. I followed the migration guide and used:

sails generate sails.io.js --force

But after running my project I get the following error on server:

Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):: Failed to parse incoming socket.io request
...
details: 'Sails v0.11.x is not compatible with the socket.io/sails.io.js client SDK version you are using (0.9.0). Please see the v0.11 migration guide on http://sailsjs.org for more information.'

I think it is something with minification of socket.io.js, because if I copy&paste actual version of _socket.io.js_ in _sails.io.js_ instead of minified code it's working.

The problem is present on Mac Chrome or Safari but Firefox is ok.

bug needs failing test

Most helpful comment

Ok i found the issue. For some reason the version is not available in the SDK META DATA information. In the file node_modules\sails\node_modules\sails-hook-sockets\lib\parse-sdk-metadata.js there is a code which checks if version is not available in the meta data then assume the version is 0.9.0

if (!handshake.query[SDK_META_PARAMS.version]) {
    handshake.query[SDK_META_PARAMS.version] = '0.9.0';
  }

So i changed 0.9.0 to 0.11.0 and voila! things are working.

All 21 comments

+1

I'm also having the exact same issue. Using the following code works as expected in Firefox, but fails in Chrome and Safari:

io.socket.request({
  method: 'get', 
  params: {}, 
  url: '/myModel'
}, function (data) {
  return console.log(data);
});

Copying and pasting the version of the socket.io client from the CDN that forms sails.io.js (v1.2.1) yields the same problem. I've also tested with minified versions of v1.3.0 and v1.3.4, and the same problem occurs.

In my experience, using a non-minified version of socket.io.js doesn't make any difference:
https://raw.githubusercontent.com/Automattic/socket.io-client/1.2.1/socket.io.js

The v0.11 upgrade introduced a breaking change to the socket.io.js client as documented here:

https://github.com/balderdashy/sails/blob/0.12/0.11-migration-guide.md#upgrade-the-socketio--sailsio-browser-client

Does upgrading the client fix the problem?

As @appmark, I'd already run the generate command to create the new sails.io.js, but the issue prevailed I'm afraid :(

I am also having the same problem Sails v0.11.x is not compatible with the socket.io/sails.io.js client SDK version you are using (0.9.0). although i have generated the new sails.io.js file using sails generate sails.io.js --force

Ok i found the issue. For some reason the version is not available in the SDK META DATA information. In the file node_modules\sails\node_modules\sails-hook-sockets\lib\parse-sdk-metadata.js there is a code which checks if version is not available in the meta data then assume the version is 0.9.0

if (!handshake.query[SDK_META_PARAMS.version]) {
    handshake.query[SDK_META_PARAMS.version] = '0.9.0';
  }

So i changed 0.9.0 to 0.11.0 and voila! things are working.

@khawerrind thanks a bunch for your help- moving conversation to the PR

@khawerrind oops misunderstood- so is this only an issue when sails.io.js is minified? I haven't run across that use case since the upgrade in the projects i've worked on and we don't have any explicit tests for it yet.

@mikermcneil on the client side when we connect with socket using io.connect() we do not pass any query parameter. But in the node_modules\sails\node_modules\sails-hook-sockets\lib\parse-sdk-metadata.js file the function is expecting query parameter to be provided otherwise it will assume the version is '0.9.0' and it will fail the request.

The code checking for version 0.11.0 is written here node_modules\sails\node_modules\sails-hook-sockets\lib\receive-incoming-sails-io-msg.js

// Grab the metadata for the SDK
var sdk = parseSdkMetadata(options.socket.handshake);
// No more backwards-compatibility for clients < v0.11.0
if (!semver.satisfies(sdk.version, '>=0.11.0')) {
      return respondWithParseError(util.format('Sails v0.11.x is not compatible with the socket.io/sails.io.js client SDK version you are using (%s). Please see the v0.11 migration guide on http://sailsjs.org for more information.',sdk.version));
    }

If i pass the same versionString as you pass in sails.io.js then everything works fine

this will fail for requests methods

var socket = io.connect();

this will work if i pass the versionString

var CONNECTION_METADATA_PARAMS = {
    version: '__sails_io_sdk_version',
    platform: '__sails_io_sdk_platform',
    language: '__sails_io_sdk_language'
};

var SDK_INFO = {
    version: '0.11.0',
    platform: typeof module === 'undefined' ? 'browser' : 'node',
    language: 'javascript'
};

SDK_INFO.versionString =
    CONNECTION_METADATA_PARAMS.version + '=' + SDK_INFO.version + '&' +
    CONNECTION_METADATA_PARAMS.platform + '=' + SDK_INFO.platform + '&' +
    CONNECTION_METADATA_PARAMS.language + '=' + SDK_INFO.language;

var socket = io.connect({
    query: SDK_INFO.versionString
});

@khawerrind gotcha-- and just verified that this is also a problem even w/ the autoconnecting socket logic when sails.io.js is minified (can send outgoing requests, but doesn't receive incoming events) Will work on it immediately this afternoon

btw I'm teaching a live course today in a couple of hours, check it out if you're around (it's free)
http://michaelmcneil.com/post/112257584857/im-teaching-a-live-sails-js-course-tomorrow

OK guys, as far as I can tell I was mistaken before-- this is not a problem, at least with the auto-connecting socket, when the latest sails.io.js client is used. Is anyone else seeing an issue there, with a fresh install of sails @0.11.x and a new app?

So then it sounds like the issue is when manually connecting? If so we can add in the versioning logic to the io.connect intercept

@mikermcneil I'm seeing this as well with a fresh install of v0.11.0 and connecting manually. If I comment out the version check, everything works as expected. I'm running in development mode, so sails.io.js is unminified.

I'm running v0.11.20 for sails-hook-sockets.

Seems that the desired behavior would be to only return the error if a version is passed in, allowing manual connections to continue unhindered but legacy sails.io.js clients will still be flagged as being incompatible.

I had the same problem as described here:
https://github.com/balderdashy/sails/issues/2720

My solution was to connect to/through io.sails. This way the version information gets passed correctly.

var socket = io.sails.connect();
socket.get('/resource', function(model) {
  // do whatever you want with it.
});

+1
I'm also having the exact same issue

I'm running into this problem while making an ios app. I removed

if (!semver.satisfies(sdk.version, '>=0.11.0')) {
      return respondWithParseError(util.format('Sails v0.11.x is not compatible with the socket.io/sails.io.js client SDK version you are using (%s). Please see the v0.11 migration guide on http://sailsjs.org for more information.',sdk.version));
    } 

from node_modules\sails\node_modules\sails-hook-sockets\lib\receive-incoming-sails-io-msg.js and it works fine, but trying to spoof the version as done above doesn't seem to work.

Can the check be removed from sails-hook-sockets? Maybe have a warning instead?

@loicsaintroch any ideas on how we can deliver a fix for this?

I run the same problem when I use sails JS version 0.11 . The socket.io does not work any more.

+1 @Peter-Barrett, the following example does not work with sails 0.11.0
https://github.com/balderdashy/sails.io.js?files=1#basic-usage-1

Thanks for posting, @appmark. I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

  • review our contribution guide to make sure this submission meets our criteria (only _verified bugs_ with documented features, please; no questions, commentary, or bug reports about undocumented features or unofficial plugins)
  • create a new issue with the latest information, including updated version details with error messages, failing tests, and a link back to the original issue. This allows GitHub to automatically create a back-reference for future visitors arriving from search engines.

Thanks so much for your help!

+1 im still seeing this error even when including the socket.io script with the and running the sails generate sails.io.js --force

Adding an update unique to my case. I was writing a client using the standard socket.io-client node module, rather than using sails.io.js from the browser. Since the metadata wasn't being added to the connection, Sails would infer that the connection was coming from a 0.9.x sails.io.js client, and threw the nasty-looking error. Previously I had hardcoded the check to substitute 0.11.x as the version when the metadata is omitted, but that ends up modifying the core code.

Ultimately I found out that you can just pass a query string in your socket.io-client initialization code when you pass in the URL, and this will satisfy Sails. In my example below, I added ?__sails_io_sdk_version=0.11.0 to the end of the connection string.

var io = client('https://' + connection.host + ':' + connection.port + '?__sails_io_sdk_version=0.11.0');

Thanks @richdunajewski, good solution!

For posterity, this is what I would recommend for anyone implementing a custom client directly from socket.io (without using the sails.io.js client). As @richdunajewski pointed out, this is the expected behavior, and what the sails.io.js client is doing under the covers, and how Sails determines how to respond (i.e. allowing it to maintain compatibility for multiple versions of the sails.io.js client). So, tldr: if you are rolling your own sails.io.js client, you'll need to send the SDK version in the query string.

_+ if you landed here because you're using React Native, see https://github.com/balderdashy/sails/issues/3882_

Was this page helpful?
0 / 5 - 0 ratings