I'm having trouble even getting the very basic socket.io sample to run. For example the first example on the welcome page of their site:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
on the server side and
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
on the client side. If I save the server-side in a host.js
file, and the client-side in a client.htm
file, and I run npm host.js
, I get
info - socket.io started
warn - error raised: Error: listen EADDRINUSE
which is already not really expected. Then, for the client.htm
(or at least that's what I think that I'm supposed to do with it -- pasting it in a client.htm file), I only get a blank screen. Not very surprising, since it starts by including a nonexisting file /socket.io/socket.io.js
, but even changing this to host.js
(which I assume it is supposed to be) doesn't change the fact that I only get a blank screen...
I'm clueless.
You can check the socket. connected property: var socket = io. connect(); console.
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 .
The bidirectional channel between the Socket.IO server (Node. js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.
EADDRINUSE means that address is already in use so it can't get the socket. Is something else already running on port 80 on your machine? 80 is commonly used by web servers.
You can also try it on some other port.
The reason you see a blank file is it doesn't connect to the node server (since it couldn't get the socket) so the on news event will never get called. It might even connecting to the socket of whatever else is running on 80 which will never emit that event :)
After you solve the port conflict, when you run the server, it should just say:
info - socket.io started
and now it's waiting for connections.
Make sure you update the htm line to your port. For example, if 81:
var socket = io.connect('http://localhost:81'); // note the :81
EDIT: I just tried it out and in the htm file I had to set the relative path to the socket.io.js file. After installing it via npm it was here in my directory.
<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
Make sure that path is relative to the htm file (doesn't start with a /). Here's how I found out where mine was:
find . -name 'socket.io.js'
On Win: dir socket.io.js /s
You should also run the host with (on *nix you may need sudo in front):
node host.js
Last thing I did when trying the sample was changing a couple lines in the htm file to this so I could see an alert message box when the event happened:
socket.on('news', function (data) {
alert(JSON.stringify(data));
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