I’m developing an application using CodeIgniter on a MAMP server (for development, live it’ll be on LAMP). I’m trying to add the real-time functionality of socket.io for a chat server, but I’m having some issues. I have socket.io and MAMP up and running independently, but I can’t get my client to talk to my server.
server.js:
// Get the Socket.io module
var io = require('socket.io');
console.log ( 'Chat Server started' );
// Create a Socket.IO instance, listen on 8084
var socket = io.listen(8084);
// Add a connect listener
socket.on('connection', function(client){
console.log ( "Server Connected" );
});
My client script (the socket.io.js loads fine and my console says “debug: served static content” whenever I load the page):
<script src="http://localhost:8084/socket.io/socket.io.js"></script>
<script type="text/javascript">
// Create SocketIO instance, connect
var socket = new io.Socket('localhost',{
port: 8084
});
socket.connect();
socket.on ( 'connect', function () { console.log ( 'Client connected' ); } );
</script>
After starting the node.js file, I get this in my console:
Chat Server started
info - socket.io started
After loading the client (directing my browser to http://localhost:8888 – the default port for MAMP), I don’t get any of the console messages, but instead a steady stream of this (about every second):
info - unhandled socket.io url
It looks like it’s not connecting at all. Also, no errors in my JS error console on the browser. Any ideas?
Running socket.io version 0.8.5, I finally got this working using the following code:
server.js
client script:
With everything up and running, upon going to my page, I see “Now connected!” almost immediately. I’m also serving my page up via CodeIgniter on MAMP – everything is working!