Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple nodeJS example not working with socket.io

Have been struggling all day trying to make this simple example work using socket.io. I've tried initially on Windows 7 with Cygwin. Have since also tried on OS X, with the same result.

When running the script, it shows this...

2 May 20:57:47 - socket.io ready - accepting connections

But visiting the index.html page doesnt show a client has even connected.

index.html

<html>
<head>
<script type="text/javascript" src="socket.io.js"></script> 
<script type="text/javascript"> 
    var socket = new io.Socket('localhost',{'port':8090});

    socket.connect();

    socket.on('connect', function(){
        console.log('connected');
        socket.send('hi!'); 
    });

    socket.on('message', function(data){ 
        console.log('message recived: ' + data);
    });

    socket.on('disconnect', function(){
        console.log('disconected');
    });
</script> 
</head>
<body></body>
</html>

server.js

var http = require('http'), io = require('socket.io'),

server = http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

var socket = io.listen(server); 
socket.on('connection', function(client){ 
    console.log('client connected');

    client.on('message', function(){ 
        console.log('message arrive');
        client.send('some message');
    });

    client.on('disconnect', function(){
        console.log('connection closed');
    });
});

Any ideas on what I could be doing wrong? No console messages whatsoever are being displayed. Notably, when i use Firebug to look at the index.html page, no scripts are being embedded, which is odd..Not sure what could be causing that.

like image 564
crawf Avatar asked May 02 '11 11:05

crawf


People also ask

How do I integrate Socket.IO in node js?

In order to do it, you need to create an index. js file and install socket.io and express. You can use the following command: touch index. js && npm install express socket.io && npm install --save-dev nodemon .

Can you use Socket.IO without node js?

Is it possible to use socket.io without any node. js dependencies? The short answer is yes. You will however have Flash dependency.

Is Socket.IO part of node js?

Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of: a Node. js server: Source | API.

Does Socket.IO need HTTP?

Websocket is created when you make upgrade from http to websocket, so it kind of does need http. socket.io isn't a pure Websocket server/implementation, it depends on HTTP for its initial connection setup.


2 Answers

You're not loading the socket.io library properly in your index.html file. Try this:

<script type="text/javascript" src="http://localhost:8090/socket.io/socket.io.js"></script> 
like image 74
noli Avatar answered Sep 21 '22 09:09

noli


You're not serving up socket.io.js (or the flash file).

I'd recomend using the CDN:

<script src="http://cdn.socket.io/stable/socket.io.js"></script>

or alternatively use express to serve the socket.io.js file.

edit:

err actually looking closer you're also not serving up index.html again express could work but for the simple example:

var fs = require('fs');
var index = fs.readFileSync('index.html');
//note the readFileSync is done only in the first tic
.
.
.
res.writeHead(200, {'Content-Type': 'text/html'}); 
res.end(index); 
like image 34
generalhenry Avatar answered Sep 19 '22 09:09

generalhenry