Egidi
Egidi

Reputation: 1776

node.js socket.io emit not working

I've got the following config:

app.set('port', process.env.PORT || 3000);
var httpserver = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

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

app.all('/blockcallback', function(req, res){

    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");

    getTxSender(req.query.tx,function(ref_wallet){
          (..)
   });
   io.emit('status', {'message': 'done'});
   res.send('*ok*');
});

index.html

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
                    var socket = io.connect('http://localhost:3000');
                        socket.on('status', function(datos){
                            alert('received');  
                        });
</script>

ok when i call http://localhost:3000/blockcallback looks like everything works perfectly, but i am not getting the 'status' call on the clientside.

my js console shows:

Error in event handler for extension.onRequest: undefined
Stack trace: undefined localhost/:1 
Error in event handler for extension.onRequest: undefined
Stack trace: undefined extensions::uncaught_exception_handler:9
Stack trace: undefined extensions::uncaught_exception_handler:9

I don't understand what's happening...

Regards,

Upvotes: 0

Views: 1952

Answers (1)

tbh__
tbh__

Reputation: 324

I think your problem is you haven't waited for the connection yet...

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

Upvotes: 1

Related Questions