Wednesday, 17 May 2017

Using socket.io for the first time

I am trying to make an web app that use Javascript and Node.js. I need to make a call from Javascript to Node.js because I must be able to pass params and I can't have them until an leaflet event is pass. I am trying to use Socket.io to pass thoses params but I can't make it to work. here is my test code that don't work. Can somebody help me figure out why it is not working?

ps. I am running on a personnal php and Node.js server.

Client-side :

    <script>
        var socket = io.connect('localhost');
        socket.on('news', function (data){
            console.log(data);
            socket.emit('my other event', {my: 'data'});
        });
    </script>
</body>

Server-side :

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
   socket.emit('news', {hello: 'world'});

   socket.on('my other event', function (data) {
      console.log(data); 
   });
});

Error from browser : Error



via CityGuy

No comments:

Post a Comment