Apache + Symfony2 + HTTPS + Node.js + Socket.io: socket.emit not firing -


i've been spending hours upon hours on problem, no avail.

edit: solution found (see answer)

project background

i'm building project in symfony2, requires module uploading large files. i've opted node.js , socket.io (i had learn scratch, might missing basic). i'm combining these html5 file , filereader api's send file in slices client server. preliminary tests showed approach working great standalone app, handled , served node.js, integration apache , symfony2 seems problematic.

the application has unsecured , secured section. goal use apache on ports 80 , 443 serving bulk of app built in symfony2, , node.js socket.io on port 8080 file uploads. client-side page connecting socket served apache, socket run via node.js. upload module has run on https, page resides in secured environment authenticated user.

the problem events using socket.emit or socket.send don't seem work. client server, or server client, makes no difference. nothing happens , there no errors.

the code

the code shown simplified version of code, without clutter , sensitive data.

server

var httpsmodule = require('https'),     fs = require('fs'),     io = require('socket.io');  var httpsoptions =  {     key: fs.readfilesync('path/to/key'),     cert: fs.readfilesync('/path/to/cert'),     passphrase: "1234lol" }  var httpsserver = httpsmodule.createserver(httpsoptions); var ioserver = io.listen(httpsserver);  httpsserver.listen(8080);  ioserver.sockets.on('connection', function(socket) {     // event gets bound, never fires     socket.on('newfile', function(data)     {         // make sure happening         console.log(data);          // process new file...     });      // oddly, 1 fire     socket.on('disconnect', function()     {         console.log("disconnected");     }); }); 

client

// twig template, i'll give excerpt {% block javascripts %} {{ parent() }}  <script type="text/javascript" src="https://my.server:8080/socket.io/socket.io.js"></script> <script type="text/javascript">      var socket = io.connect("my.server:8080",                 {                     secure: true,                     port: 8080                 });       // imagine function initiating file upload     // file object containing metadata file like, filename, size, etc.     function uploadnewfile(file)     {          socket.emit("newfile", item);     }  </script> {% endblock %} 

so problem is...

of course there's more application this, i'm stuck. page loads without errors, emit events never fire or reach server (except disconnect event). i've tried message event on both client , server check if problem custom events, didn't work either. i'm guessing blocking client-server communication (it isn't firewall, i've checked).

i'm @ loss here, please help.

edit: solution found (see answer)

after painstaking debugging, i've found wrong setup. might share findings, although (i think) unrelated node.js, socket.io or apache.

as mentioned, question had simplified code show setup without clutter. was, however, setting client through object, using properties configure socket connection. so:

var myproject = {};  myproject.uploader = {     location: 'my.server:8080',     socket: io.connect(location,             {                 secure: true,                 port: 8080,                 query: "token=blabla"             }),     // ...lots of properties , methods } 

the problem lay in use of location property name. reserved word in javascript , makes strange behaviour in case. found strange object's property name can't reserved word, decided test. had noticed referencing property incorrectly, forgot use this.location when connection socket. changed this, test.

var myproject = {};  myproject.uploader = {     location: 'my.server:8080',     socket: io.connect(this.location,             {                 secure: true,                 port: 8080,                 query: "token=blabla"             }),     // ...lots of properties , methods } 

but no avail. still not getting data on socket. next step seemed logical in frustration-driven debugging rage. changing property name fixed everything!

var myproject = {};  myproject.uploader = {     socketlocation: 'my.server:8080',     socket: io.connect(this.socketlocation,             {                 secure: true,                 port: 8080,                 query: "token=blabla"             }),     // ...lots of properties , methods } 

this approach worked perfectly, getting loads of debug messages. success!! whether expected behaviour in javascript override (or whatever happening here, "to misuse" feels better way of putting me right now) object properties if happen use reserved word, don't know. know i'm steering clear of them on!

hope helps out there!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -