Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket IO | How to get the client transport type on the serverside?

I need to know what transport method a client is using for some conditional statements on the nodeJS serverside.

Does anyone know how I can get that information? Is it held within the client object?

like image 987
wilsonpage Avatar asked Jun 08 '11 14:06

wilsonpage


People also ask

What is Socket.IO transport?

transports ​The low-level connection to the Socket.IO server can either be established with: HTTP long-polling: successive HTTP requests ( POST for writing, GET for reading) WebSocket.

How do I connect Socket.IO to client side?

var socket = require('socket. io-client')('ws://ws.website.com/socket.io/?EIO=3&transport=websocket'); socket. on('connect', function() { console. log("Successfully connected!"); });

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

How do you check if socket IO client is connected or not?

You can check the socket. connected property: var socket = io. connect(); console.


2 Answers

As of Socket.IO 1.0:

Client:

socket.on('connect', function() {
    console.log(socket.io.engine.transport.name);
}

Server:

io.sockets.on('connection', function(socket) {
    console.log(socket.conn.transport.name);
}
like image 97
jrbedard Avatar answered Oct 13 '22 02:10

jrbedard


In socket.io 0.7.6

io.sockets.on('connection', function(client) {
    console.log(io.transports[client.id].name);
});
like image 34
mak Avatar answered Oct 13 '22 02:10

mak