So I have this very basic socket.io setup that you have probably seen a thousand times already.
Please not that here that the files are served via apache.
server (app.js)
var io = require('socket.io').listen(8080);
io.sockets.on('connection', function(socket){
socket.emit('server ready', {msg: 'hi'}) ;
socket.on('random event', function(data) {
console.log('received');
})
});
and client
$(document).ready(function() {
var socket = io.connect('http://127.0.0.1:8080/projectname/server/');
socket.on('server ready', function(data){
console.log('server ready!');
});
socket.emit('random event', {hurr: 'durr'});
});
However, the only output I get is
debug - websocket writing 5:::{"name":"server ready","args":[{"msg":"hi"}]}
in the node console and nothing in the client console. Which is wrong.
I have tried the basic example from the socket.io website and it shows exactly the same behaviour. It logs the emitted data in the node console but nothing else seems to happen.
Edit: Upon further investigation, visiting the site in Firefox creates a different output in the node console:
info - handshake authorized 178702677759276313
debug - setting request GET /socket.io/1/xhr-polling/178702677759276313?t=1339080239192
debug - setting poll timeout
debug - client authorized for
debug - clearing poll timeout
debug - xhr-polling writing 1::
debug - set close timeout for client 178702677759276313
debug - xhr-polling received data packet �17�1::/stock/server/�66�5::/stock/server/:{"name":"random event","args":[{"hurr":"durr"}]}
debug - setting request GET /socket.io/1/xhr-polling/178702677759276313?t=1339080239263
debug - setting poll timeout
debug - clearing poll timeout
debug - xhr-polling writing 5:::{"name":"server ready","args":[{"msg":"hi"}]}
This looks like the data emitted fromt he client actually reached the server. However, it didnt seem to solve the actual problem: the console.log lines and both the client and the server side arent executed. This output is from Firefox 5.0.1 where it seems to fall back to xhr.
Thanks a lot in advance!
If your projectname
is stock
, then that's your problem. Socket.IO is thinking you're using a namespace. You can see this in the xhr-polling received data packet
log line. console.log
is never called since you're not listening on that namespace on the server side.
You should get rid of /projectname/server
from your client connect address and reference the client side library properly so you don't get a 404. Whether that's an Apache proxy issue or fixing the script src
in your header depends on your current setup. I can't tell from the code you've provided.
PS: Socket.io should serve the client library at http://127.0.0.1:8080/socket.io/socket.io.js
, but you might run into a cross-domain origin policy issue by referencing that asset from a document served by your apache server at port 80. Quick fix could be to serve the client lib from your apache server, which is in the socket.io-client
module dist
folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With