0

I have spent literally all day visiting tutorial websites explaining how to use nodejs and sockoets.io but I'm not able to get anything to work.

I have managed to at least run a js file:

node filename.js

But it doesnt fully work. It runs until it reaches the "var server = net..." line since the "console.log("hello")" line DOES NOT EXECUTE:

var net = require('net');

var server = net.createServer(function (socket) {
    console.log("hello");
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});
console.log("hello");
server.listen(1337, '127.0.0.1');

This i got from the official node.js site home page: http://nodejs.org/

All tutorials claim that its just so easy.

I have just tried to follow this tutorial to the letter although a lot of them skim over the part I'm stuck with (the actual installing): http://tutorialzine.com/2012/08/nodejs-drawing-game/

so following the above tutorial i run app.js from the console and i get a message "socket.io started", I get stuck at the part where it asks for you to go to this URL:

http://localhost:8080

The browser attempts to go there but it hangs for a few minutes then says: "No data received Unable to load the webpage because the server sent no data."

I have no idea how node.js works and there don't seem to be explanations as to how it works... Where is node.js installed? If its meant to be on the server, how does it get installed on the server? where should I install it to test locally? what is socket.io? where should that be installed?

All I seem to get on node.js info sites are code block dumps with little explanation as what is going on.

I followed a youtube tutorial where the guy was using WAMP server, so I thought maybe I needed to put files on a server, so I installed WAMP and disabled IIS8 server. Another note, when going to "localhost" on my browser it says "it works!" which seems like an automating message from a local server - I thought it was IIS8 but even though I disable the service, that message displays. Even if I install WAMP and have it running that message displays. Also, WAMP doesn't work either, since php files don't run. Localhost always takes me to a page displaying that message.

Is this a local server issue?

user1809104
  • 707
  • 2
  • 8
  • 26
  • On reading your question, I realize you have little idea about servers. WAMP(Apache) IIS and node.js all are servers. Just pick one and learn. Look at the FAQ for them to know more. For node.js see http://stackoverflow.com/questions/1884724/what-is-node-js and http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js – user568109 May 04 '13 at 21:24

3 Answers3

3

it is hard to give an "answer" to your question(s). I would recommend you start with a much more basic introduciton that the drawing game. Also, I would suggest you start with nodejs as is, without using socket.io right away. when you understand how node works, you can start with websockets.

Here is some node 101 stuff:

You should not need WAMP at all. nodjs is the server!

It seems that you have no idea what ports are. Your node script start a webserver that listens on port 1337. If you want to see what that server serves, you need to point your browser to localhost:1337 (not port 8080, as you tried)

luksch
  • 11,497
  • 6
  • 38
  • 53
  • 1
    Thanks for those links luksch, I'm reading the first one and have got something to work (Hello world). It is true that I don't know what ports are but the port 8080 piece of code was for a different tutorial that does listen for port 8080. I apologize I just don't understand how node.js is the server. I know youre right, but it makes no sense in my mind. I'll just have to except the magic and move forward. – user1809104 May 04 '13 at 18:00
  • I figured that you just took the lines from different tutorials. nodejs is actually not very magic. It just brings the javascript language to the server. once you understand the event loop everything falls into place. a server is just a program that runs on a computer. Nothing more, nothing less. – luksch May 04 '13 at 19:03
3

I have created a basic gist on github for using socket.io + node + express

The minimum working environment for making socket.io app is this :

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    io = require('socket.io').listen(server);

app.get('/', function(req, res) {
  res.send('<!doctype html> \
            <html> \
            <head><meta charset="utf-8"></head> \
            <body> \
                 <center>Welcome to <strong>socket.io</strong></center> \
                 <script src="/socket.io/socket.io.js"></script> \
                 <script> \
                    var socket = io.connect(); \
                    socket.emit("message", "Howdy"); \
                    setInterval(function () { \
                        socket.emit("message", "Ping"); \
                    }, 1000); \
                 </script> \
            </body> \
            </html>');
  });

  io.sockets.on('connection', function (socket) {
     socket.on('message', function(msg) {
        console.log(msg);
     });
  });

  server.listen(8000);
drinchev
  • 19,201
  • 4
  • 67
  • 93
0

you need to require('socket.io') and then create a connection io.sockets.on('connection', function (socket) in order to make it work

V31
  • 7,626
  • 3
  • 26
  • 44