0

How would I go around creating an auto-updating newsfeed? I was going to use NodeJS, but someone told me that wouldn't work when I got into the thousands of users. Right now, I have it so that you can post text to the newsfeed, and it will save into a mysql database. Then, whenever you load the page, it will display all the posts from that database. The problem with this is that you have to reload the page everytime there is an update. I was going to use this to tell the nodejs server someone posted an update...

index.html

function sendPost(name,cont) {
    socket.emit("newPost", name, cont);
}

app.js

socket.on("newPost", function (name,cont) {
    /* Adding the post to a database
     * Then calling an event to say a new post was created
     * and emit a new signal with the new data */
});

But that won't work for a ton of people. Does anyone have any suggestions for where I should start, the api's and/or programs I would need to use?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Beaurocks16
  • 341
  • 1
  • 7
  • 16

1 Answers1

2

You're on the right track. Build a route on your Node webserver that will cause it to fetch a newspost and broadcast to all connected clients. Then, just fire the request to Node.

On the Node-to-client front, you'll need to learn how to do long polling. It's rather easy - you let a client connect and do not end the response until a message goes through to it. You handle this through event handlers (Postal.JS is worth picking up for this).

The AJAX part is straightforward. $.get("your/node/url").then(function(d) { }); works out of the box. When it comes back (either success or failure), relaunch it. Set its timeout to 60 seconds or so, and end the response on the node front the moment one event targetted it.

This is how most sites do it. The problem with websockets is that, right now, they're a bit of a black sheep due to old IE versions not supporting them. Consider long polling instead if you can afford it.

(Psst. Whoever told you that Node wouldn't work in the thousands of users is talking through their asses. If anything, Node is more adapted to large concurrency than PHP due to the fact that a connection on Node takes almost nothing to keep alive due to the event-driven nature of Node. Don't listen to naysayers.)

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
  • The AJAX call assumes jQuery is being used, by the way. Change as necessary. – Sébastien Renauld May 03 '13 at 00:09
  • Could you explain the "$.get("your/node/url").when(function(d) { });" more? – Beaurocks16 May 03 '13 at 00:22
  • @Beaurocks16: Okay. jQuery allows you to use a shorthand to call AJAX methods. `$.get("your/node/url").then(function(d) {});` is exactly equivalent to: `$.ajax({url:"your/node/url", type: "GET", success: function(d) {}, error: function(d){} });`. Note that I made a mistake - it is `then` and not `when`. This performs a GET call to an URL your/node/url, which is a handy way to get data. – Sébastien Renauld May 03 '13 at 00:25
  • Is the "your/node/url" for the app.js (The node server) or another file? Thanks in advanced – Beaurocks16 May 03 '13 at 00:31
  • It is a relative URI. Make it to the same server, and make it hit your node server (depending on what webserver you're running on, you should be able to do this using some sort of proxypass). The URI **MUST** go to the same hostname, same port and same protocol, anything else will cause a security error without CORS headers. Basically, it's an URL. :p – Sébastien Renauld May 03 '13 at 00:32
  • Anytime. If you want to venture down that road, and you get stuck, look for the keywords `long polling` and `AJAX`. They'll point you in the right direction to look for - especially the first. If you'd like a ready-made solution, consider the Meteor framework. – Sébastien Renauld May 03 '13 at 00:39
  • (Also, I will be writing something very similar using the very same tools within the next week. When I'm done - would you like me to ping you with the source so you can see if it helps in any way?) – Sébastien Renauld May 03 '13 at 00:42