balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

io.sails.connect() cannot connect to multiple hosts, does not queue requests #4581

Open istrau2 opened 5 years ago

istrau2 commented 5 years ago

Sails version: N/A Node version: N/A NPM version: N/A DB adapter name: N/A DB adapter version: N/A Operating system: Windows


This issue is related to the sails.io.js library. We are using it on the front end along with webpack to connect via ws. We've recently added a separate server that is also ws enabled. Now, we are trying to setup the front end socket client to connect to BOTH ws enabled servers:

What we tried: Using the documentation, here is the code we tried (dumbed down to convey the essence):

import socketIOClient from 'socket.io-client';
import sailsIOClient from 'sails.io.js';

const io = sailsIOClient(socketIOClient);
io.sails.autoConnect = false;
window.s1 = io.sails.connect(url1, {transports: ['websocket']});
window.s2= io.sails.connect(url2, {transports: ['websocket']});

window.s1.get('/Account/' + this.accountId + '/Subscribe', {}, (resData, jwRes) => {
    ...
});
window.s2.get('/Account/' + this.accountId + '/Subscribe', {}, (resData, jwRes) => {
    ...
});

Basically, we are connecting to the two hosts and then immediately sending a connection on both sockets.

Expected result: Ideally, both sockets would connect to their respective hosts and then, after successful connection, send the messages (which the client is supposed to queue until it successfully connects).

Actual observed result: Instead, two problems occur:

  1. Observing in the network tab, we see two ws connections established with the SAME host url2.
  2. Even after successful connection, the requests (that should have been queued up) are not sent on EITHER socket (i.e. no requests to '/Account/' + this.accountId + '/Subscribe' are made via the ws connections).

Questions We have the following questions:

  1. How does one use the sails.io.js client library to connect to multiple hosts?
  2. When using the manual connect method, how does one issue a request immediately once the socket is connected?

Thanks in advance

sailsbot commented 5 years ago

@istrau2 Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

istrau2 commented 5 years ago

@mikermcneil @sgress454 Would be great if we could get an answer relatively quickly as this is a blocker for us. Thank alot.

johnabrams7 commented 5 years ago

@istrau2 Hey, I haven't tried this myself but in case this helps, here's the relevant source code: https://github.com/balderdashy/sails.io.js/blob/88b96ea019db4a03a1fb5fff0600dc7fd23f7862/sails.io.js#L635-L1045

Thanks for taking the time to put this together so coherently, much appreciated! 👍

istrau2 commented 5 years ago

@johnabrams7 Hi, thanks for response. Can you please expand a little bit on the source code that you linked to?

It looks like the source to the sails.io.js library...

rachaelshaw commented 5 years ago

@istrau2 looks like conceptually that usage should work. To diagnose what's going on, it'd be great if you would provide your version info for:

- sails

Thanks!

istrau2 commented 5 years ago

@rachaelshaw I solved this a while ago so I don't recall the exact solution. I am pretty sure the issue is that the sockets need to be connected in series.

Something like:

//connect socket one
socketOne.on('connect', function() {
          //connect socket two
});

Otherwise the sails socket client gets confused.

rachaelshaw commented 5 years ago

@istrau2 thanks for sharing your solution!