I was able to get the basic socket.io server application running on my own server, and request it directly through any web browser (I tried FF, chrome, and IE7 which all worked).
Now, the issue comes in that the client sample code doesn't work for me, and I get the following error in the javascript console in chrome:
"Uncaught ReferenceError: require is not defined" in reference to this line of code in socket.io.js: var client = require('socket.io-client');
This leads me to believe that it doesn't recognize the require command period, which seems odd. A couple of other things - I have apache running, and so moved all of my socket.io files into my apache directory htdocs to be accessed through http port 80 which were installed using cygwin and the guide at: https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows)
The socket.io files were also installed under the cygwin directory on my c: drive in windows, where they are not useful if accessed by apache. One other tidbit - I do have a socket.io-client.js file, but when I opened it to edit using wordpad, it looks corrupted, having only one line of text inside: <symlink>ÿþi
Problem: the socket is not able to connect Possible explanations: You are trying to reach a plain WebSocket server. The server is not reachable. The client is not compatible with the version of the server.
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 try the code below:io.to(socket.id). emit("event", data); whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people.
The require() function is a feature of Node.js and only works on the Javascript that is run on the server side. To include files in the browser, you would have to use the regular method:
<script src="/socket.io/socket.io.js"></script>
Node.js is usually set up in a way that the socket.io server is attached to an instance of web server, which is also part of the Node.js server. Code examples taken directly from the socket.io "how to use" page, this would be on the server side:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
If used as above, Node.js is the server that also serves the static part of the web page, and address of Node.js server is the reference for including client side scripts.
Another use case is when the static html is served by your main web server and you are trying to connect to a Node.js instance that might be in another address or another port or both. Socket.io.js is not served by your main web server. It is served directly by the socket.io running on the Node.js server. You have to provide client browser the Node.js servers address to get the socket.io client side Javascript file, like this:
<script src="http://nodejs.address:port/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://nodejs.address:port');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
As a side note, there are client side javascript libraries that provide require() function, check Javascript require on client side
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