0

I am trying to learn Node and build a simple chat application. It seems like everyone uses socket.io. I would like to understand how to do this on a more fundamental level using get and post.

Basically, all I want to do is have a form that takes an input and reposts it below the form for everyone to see.

This is what I have so far:

//Requirements
var express = require('express'); 
var app = express(); 

//GET
app.get('/', function (req, res) {
 // res.send('Hello World!');
        var response =
          "<HEAD>"+
                  "<title>Chat</title>\n"+
          "</HEAD>\n"+
          "<BODY>\n"+
                    "<FORM action=\"/\" method=\"get\">\n" +
                            "<P>\n" +
                                   "Enter a phrase: <INPUT type=\"text\" name=\"phrase\"><BR>\n" +
                                   "<INPUT type=\"submit\" value=\"Send\">\n" +
                           "</P>\n" +
                   "</FORM>\n" +
          "<P>phrase</P>\n"+
          "</BODY>";
        var phrase = req.query.phrase;
        if(!phrase){
                res.send(response);
        }else{
                res.send(response);
                res.send(phrase);
        }
});

//For testing
app.get('/test', function(req, res){
        res.send('I am a robot');
        console.log('told visiter I am a robot');
});

//Run the app
var server = app.listen(8080, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('App listening at http://%s:%s', host, port);
});

I've been trying a bunch of things, but I am pretty stumped.

Ethan
  • 1
  • 1
  • 1
    Get and post only post back or get back the result to the client who requests it and not all. You need web sockets to send the response to everyone and socket.io is the best at it. – Zee Apr 12 '15 at 05:02
  • Is there no way to have node update the html with the result? Or would that require a page refresh? – Ethan Apr 12 '15 at 05:05
  • there are other methods than sockets for pushing out data to connected clients. it's trivial to write an EventSource (SSE) server using the http module, you can delay the response of http returns, and you could even use comet or jsonp long-polling. or, just hammer poll like all the php chat scripts do... – dandavis Apr 12 '15 at 05:09
  • (At least some of) the methods described by dandavis such as long polling and hammer polling are used specifically to get around traditional request response limitations of http. Theres probably a good reason why everyone uses socketio :) – Dave Pile Apr 12 '15 at 05:17
  • @dandavis Thanks! That was the direction I was looking to be pointed in. Do you have any examples of how to do this in node without using libraries that abstract away the task? – Ethan Apr 12 '15 at 05:24
  • some downsides to sockets: unfamiliar interaction pattern (not req/res), DIY routing/auth, difficulty connecting non-browsers such as existing PHP boxes, no IE9< support, firewall/ ported wifi difficulties, and potentially increased resource usage (ram, open connections). – dandavis Apr 12 '15 at 05:26
  • @DavePile Ah okay. I'll work my way to socketio, but I want to understand the basics first, even if it is over complicated – Ethan Apr 12 '15 at 05:27
  • webSockets were invented for a reason - they are particularly good at pushing data from server to client and http requests are particularly not efficient at pushing data asynchronously from server to client. So. it's a fine goal to want to understand how get and post requests work, but not particularly useful to try to use them to solve a problem that they're particularly bad at when webSockets are really the right tool for the job. – jfriend00 Apr 12 '15 at 05:33
  • 1
    i would recommend EventSource to get started. it's an always-on way to instantly push data to the client. since http can push to the server anytime, you only need that live connectivity one-way (S->C). a simple node implementation snippet to go in an existing http-based script: http://jsfiddle.net/Lr9omhts/1/ then the client is really easy, see docs for examples. – dandavis Apr 12 '15 at 05:35
  • +1 for [SSE](https://html.spec.whatwg.org/multipage/comms.html#the-eventsource-interface). The client-side is widely supported and robust, and the server-side is quite easy to implement on node.js without any 3rd-party library on either side. Replace SSE with Websocket later when you feel comfortable. – Touffy Apr 12 '15 at 06:09

1 Answers1

0

Did you hear about messaging backend jxm.io?

It works with JXcore (open sourced fork of Node.JS). JXM itself is an open source project, which you can find on github: jxm.

It's really fast and efficient, you can check some tutorials. For example, below is minimal code, that you need to run on server-side:

var server = require('jxm');
server.setApplication("Hello World", "/helloworld", "STANDARD-KEY-CHANGE-THIS");
server.addJSMethod("serverMethod", function (env, params) {
   server.sendCallBack(env, params + " World!");
});
server.start(); 

The client's part can be found here: Browser Client (JavaScript)

JXM also supports Java clients (runs on android) and node clients.

infografnet
  • 3,749
  • 1
  • 35
  • 35