Question

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?

Was it helpful?

Solution

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();

OTHER TIPS

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

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>

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()

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top