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?
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 .
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.
You can check the socket. connected property: var socket = io. connect(); console.
Try listening on the server after you bind it with socket.io
Place this
httpServer.listen(8253);
after
var webSocket = socketIO.listen(httpServer);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With