socket io server base url code example

Example 1: socket io query

// Client side
const socket = io({
  query: { token: 'cde' }
});

// Server side
io.on('connection', (socket) => {
  let token = socket.handshake.query.token;
  
  // Do something...
});

Example 2: socketio connect websockets

var ws = new WebSocket('ws://localhost/socket.io/?EIO=3&transport=websocket');
ws.send('42' + JSON.stringify(['hello', 'there']));
// ws.onmessage will get a MessageEvent object with the data property being encoded in the similar way.

Tags:

Html Example