0

I need to pass the value of the PHPSESSID cookie when a client try to connect to a socket.io websocket.

I tried to use socket.handshake.headers, but for some reason the PHPSESSID was not passed.

I tried to emit a connect event to send the cookie value. But I still can't seems to be able to do this.

Here is what I have done

This is my websocket server code

var env = require('./config');

var app = require('express')();
var server = require('http').Server(app);

var io = require('socket.io')(server);

var clients = [];

server.listen(env.socket.port, env.socket.host, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Websocket running at http://%s:%s', host, port);
});

app.get('/', function (req, res) {
    res.send('Welcome!');
});

io.on('connection', function (socket) {

    //console.log(socket.handshake.headers);
    clients.push(socket);

    socket.on('connect', function(msg){
        console.log('PHPSESSID: ' + msg);
    });

    socket.on('chat', function(msg){
        console.log('message: ' + msg);
    });

    socket.emit('chat', { hello: 'world' });
});

Here is my client code

<script>

    $(function(){
       var socket = io.connect('https://10.0.4.18:8020');

       socket.emit('connect', { 'sessionId' : '<?php echo $_COOKIE['PHPSESSID']; ?>' });

      $('#f').click(function(e){
            e.preventDefault();

            if($('#m').val() == '')
                return;

            socket.emit('chat', $('#m').val());
            $('#m').val('');
            return false;
      });

      socket.on('chat', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    });

</script>

I am expecting to print the json string with the sessionId to the server's console.

How can I pass/access the PHPSESSID value from the websocket?

Junior
  • 11,602
  • 27
  • 106
  • 212
  • If you're looking to share Session ID between Node and PHP, a quick search turned up existing SO results: http://stackoverflow.com/questions/23795926/share-sessions-between-php-and-node – TbWill4321 Sep 14 '15 at 21:34
  • @TbWill4321 thank you for that? can't I just pass the cookie value to my websocket? my session in stored in MySQL database. so if I can pass this values I will do a MySQL lookup and validate the session in nodejs – Junior Sep 14 '15 at 21:39

0 Answers0