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?
Try listening on the server after you bind it with socket.io
Place this
httpServer.listen(8253);
after
var webSocket = socketIO.listen(httpServer);
Edited: Apologies, I have written something that did not answer your question.
On the client side you need the following:
var socket = io.connect(); //Hostname and port not required - Autodetected
socket.on('connect', function(){
$('#status').text('Connected');
});
socket.on('message', function(m){
$('#message').text(m);
});
socket.on('disconnect', function(){
$('#status').text('Disconnected');
});
Working example => https://github.com/parj/node-websocket-demo/blob/master/public/main.js
NPM Information (if required): If you are in Linux
cd <location of your server.js>
npm install -g socket.ion #install globally
npm link socket.io. #Create a symbolic link
If you are on Windows you can't do npm link
cd <location of your server.js>
npm install socket.io
Your directory structure should look like
server.js
node_modules/ #Directory - same level as server.js
socket.io #socket.io underneath that
node_modules should be in the same directory as server.js, not above server root
When you are converting from a regular express app:
const express = require('express')
const app = express()
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
It is important to do two things:
One(that one I believe everyone gets right):
var server = require('http').Server(app);
var io = require('socket.io')(server);
Two(this one is very easy to miss) :
Call server.listen
instead of app.listen
I spent almost two hours debugging this, that's why I'm documenting.
For those deploying on Azure (I can't vouch for any other platforms) make sure that your package.json file includes a start script.
Example:
"scripts" : { "start": "node index.js" }