I am working on a simple Node.js bi-directional client \ server communication channel, and I am trying to use socket.io on the server and socket.io-client on the client.
I pasted the code below, as you will see some fairly simple material, and I am running both on my local computer - to minimize complexity.
The behavior that I see is:
- I am starting the server.
- In the server console, the server is running on the console.
- I am running a client.
- Client Log Server Ready! ''
- Nothing else happens ...
What I expect is a server for registration "Client is ready!" and then the contents (“Finished Received”).
I even used WireShark to sniff the line, and it looks like the client is emitting a message as it was designed, but the callback on the server does not work.
I am running node v0.8.4 with express v3.1.0, socket.io v0.9.13 and socket.io-client v0.9.11 (all installed via npm).
Here is the server code ...
var http = require('http'), express = require('express'), app = express(), server = http.createServer(app); app.configure(function(){ app.use(express.static(__dirname + '/public')); }); server.listen(8080); console.log("Server started"); var io = require('socket.io').listen(server); io.sockets .on('connection', function(socket){ socket.emit('server ready', { msg: 'ready' }) ; }) .on('comms', function(content) { console.log('Client is ready!'); console.log(content); });
And here is the client code ...
var clientio = require('socket.io-client'); var socket = new clientio.connect('http://localhost', { port: 8080 }); socket .on('server ready', function(data){ console.log('Server is ready!'); socket.emit('comms', 'Ready received'); }) .on('connect-error', function(error) { console.log('Connection Error\n' + error); }) .on('error', function(error) { console.log('Socket Error\n' + error); })
The documentation and examples for socket.io and socket.io-client are somewhat confusing (to be charitable) and they seem like a slightly moving target ... but from what I can say, I think this should work.
I hope someone can give me advice on where I am going wrong?