node.js & socket.io

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

node.js & socket.io

Post by Torniquet »

yo guys! having issues with these 2 which i am hoping someone will be able to help me fix.

I am running this all on a vps which had a clean install of the latest centos. all the web server guff is all working fine and dandy, but I cannot find a huge amount of info on using node.js and socket.io from a vps on a website.

I followed these 2 guides (serverfault to do node.js and the other for the rest) changing bits to use updated files.
http://serverfault.com/questions/299288 ... -on-centos
http://spiritconsulting.com.ar/fedex/20 ... socket-io/

Then remembering Halls made an install vid, I went and watched them and followed the directions in video 7 (node.js / socket.io vid 2) and moved all the relevant files into the web directory.

My problem now is that I am getting the following error in the error console of the web browser. Uncaught ReferenceError: require is not defined

I got no clue what is going wrong, I have tried all the bits i can find on the net with no success...

If anyone has ANY clue on what is going on please let me know.

The index page...

Code: Select all

<html>
<head>
<script src="http://domain.net:80/domains/test/socket.io/socket.io.js"></script>
<script>
var http = require( 'http' );
 
var server = http.createServer( function( req, res ) {
    //http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    res.writeHead( 200, { 'content-type': 'text/plain' });
    res.end( 'Hello World!' );
});
 
server.listen( 8080 );
</script>

</head>
<body>

</body>
</html>

im new to vps stuff and my boss dont know anything about node.js and socket.io so is not much help at this point lol

ta x
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: node.js & socket.io

Post by Jackolantern »

That is really strange, because to my knowledge require() is an in-binary library function of Node (there is no js file for it to be found; it is just supposed to be there).

Have you tried to go back down to the basics? Uninstall everything, install just Node and then try a simple HTTP server:

Code: Select all

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);
That should be done first to ensure that Node is running correctly on your server. If it is, then you should be able to browse to www.yourdomain:8888.com and you should get a white, nearly empty page that says "Hello World" on it.

Try that! :)
The indelible lord of tl;dr
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: node.js & socket.io

Post by Jackolantern »

Wait a second. I just really went back in and looked at your code. I don't understand it at all. It looks like Node put the Node.js server code in the middle of an HTML page. I have never seen Node code run that way. Require() is being run on the browser-side, and there is no require() function without RequireJS files (which is not what you need here), so that is why it blew up with errors right when it hit that. If that was removed, it would just blow up on the next line as it hit more Node-only functionality.

What does your actual server code look like? I am assuming what you pasted into the code block here is just what the browser spit out on the client-side.
The indelible lord of tl;dr
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: node.js & socket.io

Post by Torniquet »

yea that code is wrong lol. that is meant to be index.html and app.js files lol.

the index.html page is

Code: Select all

<html>
<head>
<script src="http://server123.2dservers.net:80/domains/test/socket.io/socket.io.js"></script>


</head>
<body>

</body>
</html>
app.js is

Code: Select all

var http = require( 'http' );
 
var server = http.createServer( function( req, res ) {
    //http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    res.writeHead( 200, { 'content-type': 'text/plain' });
    res.end( 'Hello World!' );
});
 
server.listen( 8080 );
my bad, been a long day lol.

when i run node from the command line, this is output

info - socket.io started

if i use the code from halls' app.js, with the above code nothing is output, but seems to run.

that require error is happening on line 12 of socket.io.js

var client = require('socket.io-client');


this is becoming a real frustration lol :(
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: node.js & socket.io

Post by Jackolantern »

You need to remove the Socket.io portion of the HTML page for now because you aren't running it server-side. I am not a Node/Socket whiz, but I believe socket.io has to be required() on the server-side before the client-side socket.io script will function. It could be causing the issues, but I am not sure. Again, I am not super experience with Node.
The indelible lord of tl;dr
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: node.js & socket.io

Post by Torniquet »

well i went back to the beginning, used halls' files from the node for beginners series. the code is exactly the same as video 2 and i am still getting Uncaught ReferenceError: require is not defined

if i do http://domain.net:8080/domains/test/soc ... cket.io.js it cannot find the file, so i have to revert it to http://domain.net:80/domains/test/socke ... cket.io.js

this is the closest result i have found for my issue... http://stackoverflow.com/questions/8202 ... ot-defined

but i am doing everything on it as it says, but still no joy -.-

Does anyone have a virtual stick we can use to poke halls with to see if he has the answer lol
New Site Coming Soon! Stay tuned :D
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: node.js & socket.io

Post by Torniquet »

OK NEW DEVELOPMENT!!

using this...

<script src="http://server123.2dservers.net:8080/dom ... "></script>
I am now getting some activity..

logs...
Uncaught SyntaxError: Unexpected token < socket.io.js:1
Uncaught ReferenceError: io is not defined /domains/test/:10

cant quite understand why im getting the syntax error :s it is the start of the html document lol.


after then changing it to this

<script src="http://server123.2dservers.net:8080/soc ... "></script>

I get the following logged.
GET http://server123.2dservers.net/socket.i ... 9535484262 404 (Not Found) socket.io.js:1628

not the slightest clue what it is trying to get there but socket.io is not on the server root, but i saw somewhere about extending a classes using that src path. I also get some activity on the node server aswell...

debug - served static content /socket.io.js


So something is certainly happening. But I am just getting myself more and more confused lol.

Any ideas anyone?


MWAHAHAHA FIXED IT BISHES!!!

guh that was a pain in the ass!! lol

just for reference i guess this is my html document

Code: Select all

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://server123.2dservers.net:8080/socket.io/socket.io.js"></script>


<script>
	var socket = io.connect('http://server123.2dservers.net:8080');
	socket.emit('testconnection');
	
	socket.on('testthis', function(data) {
		alert(data);
	});
</script>
</head>
<body></body>
</html>
Note all files are actually contained in a sub directory of /domains/test.
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: node.js & socket.io

Post by Jackolantern »

Glad you got it working :)
The indelible lord of tl;dr
Post Reply

Return to “Coding”