Kim
Kim

Reputation: 1198

Socket.io - Cannot load file or io is not defined

I'm trying to set up a simple NodeJS - socket.io web application.

I have installed NodeJS and Socket.io on my Raspberry pi and I can start the server script without any problems.

I have downloaded the client socket.io, which I'm loading in the frontend script.

My problem is, whenever I try to connect to my server my browser gives me these errors.

Error: failed to require "socket.io" from "root"
...plete}}});require.register("learnboost-engine.io-client/lib/transports/websocket...

socket.io.js (linje 1)
ReferenceError: io is not defined

var socket = io.connect('http://192.168.0.105:8888');

Here is the server script.

var io = require('socket.io').listen(8888);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
console.log('Running fine');

Here is my frontend script.

<!--<script src="localhost:8080/socket.io/socket.io.js"></script>-->
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://192.168.0.105:8888');
    socket.on('news', function (data) {
      console.log(data);
      socket.emit('my other event', { my: 'data' });
    });           
</script>

My Raspberry pi is hosting the nodejs - socket.io, and the website is hosted on my main computer with Wampserver.

I really hope someone can help me :) Thanks in advance.

Upvotes: 1

Views: 973

Answers (2)

MartianMartian
MartianMartian

Reputation: 1849

it says here that

var socket = io()

connects to the host server by default, which means you don't have to put anything in there

http://socket.io/get-started/chat/

Upvotes: 0

el vis
el vis

Reputation: 1302

Socket.io.js is hosted by NodeJS server, so you should connect to

<script src="192.168.0.105:8888/socket.io/socket.io.js"></script>

And

http://192.168.0.105:8888 is wrong, http means that you want to use port 80 .You should use 192.168.0.105:8888 instead.

Upvotes: 2

Related Questions