socket.io within express (connection tracking)

99 views
Skip to first unread message

Phil Daws

unread,
Feb 10, 2012, 9:38:11 AM2/10/12
to nod...@googlegroups.com
Hello,

working my way through my first app and now attempting to emit and event back to the connected client through socket.io.  On the server side I have the following to capture the connecting clients IP address into an array (later I will create a unique ID on the client side as-well in-case they have two browser sessions open):

var connections = {};

io.sockets.on('connection', function(socket) {
  var client = socket.handshake.address;

  connections[client] = socket;

  logger('Connection from ' + client.address);

  socket.on('disconnect', function() {
    logger('Disconnection from ' + client.address);
  });
});

The connected client then performs an action, in this case attempts a file upload, for which I need to send a progress status so I tried the following:

app.post('/upload', function(request, response) {

  var ipAddress = request.connection.remoteAddress;

  logger('URL request came from: ' + ipAddress);

  var form = new formidable.IncomingForm(),
    files = [],
    fields = [];

  form.uploadDir = __dirname + '/uploads';  
  
  logger('Processing form');

  form
  
    .on('progress', function(bytesReceived, bytesExpected) {
      var progress = (bytesReceived / bytesExpected * 100).toFixed(2);
      var MB = (bytesExpected / 1024 / 1024).toFixed(1);     
      logger('Uploading ' + MB + 'MB (' + progress + '%)');
      connections[ipAddress].emit('status', { completed: progress });
    })

.....

when I run it through I receive the error "TypeError: Cannot call method 'emit' of undefined" when I upload a file.  My understanding was that the connections array would be holding the socket object and that is what I would emit against ?

All help gratefully received and appreciated.
--
Thanks, Phil

Phil Daws

unread,
Feb 10, 2012, 10:59:26 AM2/10/12
to nod...@googlegroups.com
Looks like I need to read, embrace and commit to memory http://www.danielbaulig.de/socket-ioexpress/ :)
--
Thanks, Phil


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply all
Reply to author
Forward
0 new messages