Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io not being served by Node.js server

As I understood it, from http://socket.io/#how-to-use, node.js automatically serves the socket.io file on the server.

I have installed socket.io with npm install socket.io and I can see that it resides in node_modules one level above the server root.

server.js:

    var static = require('./plugins/node-static');
var socketIO = require('socket.io');
var clientFiles = new static.Server('./client');

var http = require('http');
httpServer = http.createServer(function (request, response) {
    request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) { .....

index.html:

<html>
<head>
    <title>Chat</title>
</head>
<body>
    <script src="/socket.io/socket.io.js"></script>
    <script type="text/javascript"
            src="http://code.jquery.com/jquery-1.5.2.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            var webSocket = new io.Socket('localhost', { port: 8253 });
            webSocket.connect(); .......

Starting the server works fine, but when opening index.html, I receive the following error:

GET http://localhost:8253/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined                 :8253/:25

Ideas?

like image 927
Soroush Hakami Avatar asked Dec 24 '11 15:12

Soroush Hakami


People also ask

How do I connect Socket.IO to 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 .

Does Socket.IO require node js?

In this guide we'll create a basic chat application. It requires almost no basic prior knowledge of Node. JS or Socket.IO, so it's ideal for users of all knowledge levels.

How do you check Socket is connected or not Nodejs?

You can check the socket. connected property: var socket = io. connect(); console.


1 Answers

Try listening on the server after you bind it with socket.io

Place this

httpServer.listen(8253);

after

var webSocket = socketIO.listen(httpServer);
like image 141
fent Avatar answered Sep 20 '22 20:09

fent