How would you create an auto-updating newsfeed without a reload?


Problem :

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?



Solution :

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.)


Mysql Tutorials

Mysql Howto..

How to deal with mutliple sites that access one 'brain'?

How to work with multiple tables and not get duplicate data? (MySQL/PDO)

How to get specific values from select menu and use her in mysql query?

How to prefix / suffix column names in a MySQL query?

How to return JSON response in below format from MySQL table in PHP

How to select all field in all tables from a MySQL in talend

How to improve SQL query w/ JOINs?

How to query avg for every past 7 days in sql, MySQL?

How to insert 1 million records into table database Oracle using cross join

How to import my hosted mysql database to my local host database?

MySQL how to select all rows by specific day (mon, tue, etc) with datetime format

How do I select one row from a MySQL table?

how to remove double qoute character in mysql

How to create combobox in php mysql?

How do I return a field in MySQL from a sub-query linked to the main query results?

How to order my MySQL records ? | MySQL - WordPress

How to count facebook likes into MySQL Database

How to delete a friendship from a MySQL db using php/pdo and check if it is successful

How to properly loop in a stored function on MySQL?

How to insert values into a mysql database using php

how to display an image from mysql database on a jsp page? [duplicate]

Doctrine2 relations - how to set inexistant associate entity object

How to improve a MYSQL Query with multiple JOINS

how to update table based on another table in mysql?

How to save comment with paragraph(new line) and fetch it from database with paragraph?

How do I make a MySQL query that's equivalent to Fusion Tables “summarize” function?

how to get ordered results in mysql

How to set up a simple while loop to run a test from MySQL workbench?

How to use concated variable into mysql query inside mysql stored procedure?

How to compare and change values in MYSQL