17

I pass a custom query string containing an ID to authorize a client on the server when he is connecting via socket.io.

var env = {id:12345};
var socket = io.connect('https://example.com', {query:'i='+env.id, port:443, reconnect:false, secure:true});  

When a client disconnects for whatever reason, he can reconnect by simply calling the following function.

socket.socket.connect();

However, at the time of a reconnect env.id usually contains a different value than during the initial connect and I want to have the current value in the query string at each reconnect.

The above reconnect method sends the initial value again, even if env.id was changed to an other value meanwhile.

I tried to set the current parameters when calling the reconnect function, but that does not seem to work.

socket.socket.connect('https://example.com', {query:'i='+env.id, port:443, reconnect:false, secure:true})  

Is there a simple way to update just the query string with the current env.id value and then reconnect?

Or do I need to destroy the entire socket object once disconnected and then build up a new connection using var socket = io.connect() with the current id?

1nsane
  • 1,017
  • 2
  • 12
  • 21
  • The documentation gives an example on how to do this: https://github.com/socketio/socket.io-client/blob/master/docs/API.md#with-query-option – Joe P May 11 '18 at 09:33

5 Answers5

13

I analyzed the socket object and found the solution, therefore I am answering my own question.

The query string is stored inside socket.socket.options.query. You can change it to the desired value directly before reconnecting.

socket.socket.options.query = 'i='+env.id;
socket.socket.connect();
1nsane
  • 1,017
  • 2
  • 12
  • 21
11

In the current version of socket.io (at the moment of writing is 1.3.7) you should use

socket.io.opts.query = 'i='+env.id;

But be careful of using this due this bug

2

Using socket.io version 1.4.8 , I tried a bunch of things to get this to work, what ultimately worked for me was to set both socket.io.uri and socket.io.opts.query before calling connect() to reconnect

// Original connection
socket = io.connect('https://socket.example.com/?token=' + data.socket_auth_token, {
                            reconnection: false,
                            transports: ['websocket', 'polling'],
                            'sync disconnect on unload': true
                  });

// Reconnect

socket.io.uri = 'https://socket.example.com/?token=' + data.socket_auth_token; 
socket.io.opts.query = 'token=' + data.socket_auth_token;
socket.connect();

And here is more code for context:

 <script src="//js/contrib/socket.io.js"></script>
    <script>

        socket = io.connect('https://socket.example.com/?token=' + data.socket_auth_token {
            reconnection: false,
            transports: ['websocket', 'polling'],
            'sync disconnect on unload': true
        });

        function reconnectSocket() {
            if (typeof socket != 'undefined' && !socket.connected) {
                $.ajax({
                    url: '/api/socket/',
                    type: 'POST',
                    data: {
                        'action': 'generate_token',
                    },
                    dataType: 'json'
                }).done(function (data) {
                    socket.io.uri = 'https://socket.example.com/?token=' + data.socket_auth_token;
                    socket.io.opts.query = 'token=' + data.socket_auth_token;
                    socket.connect();
                });
            }
            setTimeout(reconnectSocket, 5000);
        }
        setTimeout(reconnectSocket, 5000);
    </script>
Dave Patrick
  • 278
  • 3
  • 8
  • This solution worked for me somehow by only updating query parameters on reconnect, query parameters were not updating but with Uri change and query change it works. Thanks – Achilles Aug 22 '17 at 05:21
  • Didn't work for me as of 2.0.3, I've tried changing socket.io.opts.query, socket.query, socket.io.uri and socket.uri :/ – Milan Velebit Mar 05 '18 at 10:44
0

For v2.1.1 I solved that by disconnecting and reconnecting after changing the query params:

socket.socket.query = { ...updated queries here}
socket.socket.disconnect().connect()
Maicon Gilton
  • 581
  • 1
  • 7
  • 6
-1

you can use reflection like this:

        Field field = Manager.class.getDeclaredField("opts");
        field.setAccessible(true);
        Manager.Options options = (Manager.Options) field.get(socket.io());
        options.query = getQuery();
JokAr
  • 1
  • 1
  • 2