0

I'm in the process of learning Node and have a question I can't seem to find the answer to. In the following example of a minimalistic chat server the node server expects the client page to reside in the same directory as the server file, if I was building a client side app for a mobile device, how would I send the data back to the proper client?

    var fs = require('fs')
    , http = require('http')
    , socketio = require('socket.io');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    **res.end(fs.readFileSync(__dirname + '/index.html'));** 
}).listen(8080, function() {
    console.log('Listening at: http://localhost:8080');
});

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});
RapsFan1981
  • 1,177
  • 3
  • 21
  • 61
  • Can you elaborate on how the communications between your server and client-side app would work? I'm not sure I understand the following: `if I was building a client side app for a mobile device, how would I send the data back to the proper client` – CgodLEY Aug 09 '12 at 00:08
  • Sorry. What I meant was if I was the client index.html was a native app compiled in Phonegap and residing on an individual's mobile, how do I route the messages back to the client? res.end(fs.readFileSync(__dirname + '/index.html')); is if index.html is on the node server – RapsFan1981 Aug 09 '12 at 00:14

1 Answers1

1

Are you running into this problem? If so, perhaps using the phonegap iOS / Android plugins for WebSockets may help you.

For reference, here is what res.end(fs.readFileSync(__dirname + '/index.html')); means:

  1. Synchronously read the contents of the file index.html (located in the server's current directory)

  2. End the HTTP response after sending the contents of the file over the network to the client who requested the page (could be the local machine, a phone, a computer on the internet -- anyone who has access to the server). In other words, node is acting as an HTTP server for the page index.html

  3. Index.html presumably contains some socket-related code that instructs the client to connect to the socket on the server (the server's socket is created by socketio.listen(server))

Community
  • 1
  • 1
CgodLEY
  • 994
  • 8
  • 16
  • I haven't run into that yet but am making note of it incase I do later on. I think my understanding of the node framework may be wrong. Does the line res.end(fs.readFileSync(__dirname + '/index.html')); tell the node server to send the response to index.html on the localhost? How do you send it to a client, another server that's not localhost – RapsFan1981 Aug 09 '12 at 00:57
  • `res.end(fs.readFileSync(__dirname + '/index.html'));` means 1) Synchronously read the contents of the file index.html (located in the server's current directory) 2) End the HTTP response after sending the contents of the file over the network to the client who requested the page (could be the local machine, a phone, a computer on the internet -- anyone who has access to the server). In other words, node is acting as an HTTP server for the page index.html 3) Index.html presumably contains some socket-related code that instructs the client to connect to the socket on the server. – CgodLEY Aug 09 '12 at 01:26
  • Ok thats what I thought. After searching some more it seems emit() is used when connecting from an outside – RapsFan1981 Aug 09 '12 at 02:17