1

What i want to be able to do is have a server which one client will send updates and multiple other clients would receive those updates and update the relevant parts of the page.

This must be really easy todo with node js, but i just dont know where to get started.

Is there anyone here that can help and give me a push of how to startup this client and server.

Thank so much!

I have looked all over for something to help me but they all end up failing....


UPDATE

I want to use socket.io and nodeJS to make the connections.

I have starter code for the server which i got online:

var http = require('http'),
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')

server = http.createServer(function(req, res){
 // your normal server code
 res.writeHead(200, {'Content-Type': 'text/html'});
 res.end('<h1>Hello world</h1>');
});
server.listen(5454);

// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
  // new client is here!
  console.log('new connection!');
  client.on('message', function(){ console.log('send message') })
  client.on('disconnect', function(){ console.log('disconnect') })
});

also i have code for the client. but it has errors:

  <script src="http://cdn.socket.io/stable/socket.io.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
 var socket = new io.Socket('localhost');
 socket.connect();
 console.log(socket);
 socket.on('connect', function(evt){ console.log('connected',evt) })
 socket.on('message', function(evt){ console.log('got message',evt) })
 socket.on('disconnect', function(evt){ console.log('disconnected',evt) })
</script>

UPDATE 2:

This is what happens when i run the server:

[nlubin@localhost websocket]$ sudo node server.js
10 Mar 17:40:49 - socket.io ready - accepting connections

ok and this is what i see in the console in chrome when i run the client:

    Unexpected response code: 301
1299796919120Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919120".
1299796919120Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919120".
1299796919130Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919130".
1299796919130Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919130".
1299796919151Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919151".
1299796919157Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919157".
1299796919162Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919162".
1299796919166Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919166".
1299796919170Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919170".
1299796919174Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919174".
1299796919177Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919177".
1299796919181Failed to load resource: the server responded with a status of 404 (Not Found)

And those XHR error keep happening

Naftali
  • 144,921
  • 39
  • 244
  • 303

1 Answers1

1

You really need to give us more context to what you are doing if you want to get a decent an answer.

I assume when you say 'client' you are referring to a web browser. And if I understand you correctly you're going to want to be 'pushing' data from the server to several web browser clients. In which case you are going to want to keep a persistent connection open between the browser and server--check out Socket.io.

The basic flow will be (1) a user visits your webpage, (2) some javascript which lives on that webpage will connect to your Node.js/Socket.io server, (3) the server can then push messages to any/all of the currently connected browsers AND the browser can send messages to the server, (4) for messages coming from the server the Javascript running on each client can interpret those messages and make the appropriate changes to the web page's DOM.


EDIT: Your client side javascript can't connect to the server. If they are running on the same machine (localhost) then that probably means the port is the problem.

Try specifying the 'port' option for your client, which in your case is '5454'.

var socket = new io.Socket('localhost',{'port':5454});

Be sure to restart the Node.js server AND refresh the webpage.

Chris W.
  • 37,583
  • 36
  • 99
  • 136
  • I tryed that, now i just keep getting: `Failed to load resource :5454/socket.io/xhr-polling//1299797743814 Failed to load resource :5454/socket.io/xhr-polling//1299797753817 Failed to load resource :5454/socket.io/xhr-polling//1299797763822 Failed to load resource :5454/socket.io/xhr-polling//1299797773827 Failed to load resource :5454/socket.io/xhr-polling//1299797783830 Failed to load resource :5454/socket.io/xhr-polling//1299797793831 Failed to load resource` over and over... (no longer have code 301 tho.) – Naftali Mar 10 '11 at 22:56
  • also i get no response on the server side that a client connected – Naftali Mar 10 '11 at 22:58
  • For whatever reason your browser doesn't seem to be able to access localhost:5454, are you running your browser on the same machine as your Node.js server? That's the only way a 'localhost' connection will work. – Chris W. Mar 11 '11 at 18:38
  • no i am not running in the same place. but i also tried replacing local host in the client js with the server name and i got the same result – Naftali Mar 11 '11 at 18:40
  • 2
    The problem is that the machine you are running the client on can't access the server (either at all or at that particular port). It sounds like you don't have the expertise to debug the networking problem yourself, so you should move your work so it is all on the same machine. This will simplify your work and allow you to use 'localhost' like with the tutorial. In the future, this is the sort of information you need to include in your original question. – Chris W. Mar 11 '11 at 21:19