Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This may be a inappropriate question, but I'm really need help demystifying this.

Suppose we want to create web application like reddit or Gmail. The way I understood it from my research is that there are two ways to approach this problem.

1) Web application like reddit can be created using PHP only. HTML for the front-end will be rendered by PHP on the server and application logic will also be written in PHP on the back-end side. With this method, web application would not feel "real-time" since for any changes you would need to refresh the page.

2) Create RESTful backend in PHP and use JavaScript frameworks like backbone.js to create single page web application and manipulate the HTML on the front-end. Front-end written in javascript would send requests to back-end which would only return JSON packages (no HTML). On the front-end, Javascript should decide how to manipulate the front-end based on the JSON packages returned from the back-end. With this method, web applications would be real-time.

What is the advantage of using method 2 over method 1? I read that Gmail is implemented using method 2. Why did they use javascript frameworks to create the front-end, if they could send HTML rendered on the back-end side and make AJAX calls to check for notifications?

Is it generally better to use method 2 over method 1? Server-side page rendering or client-side page rendering?

Thanks in advance!

share|improve this question

closed as primarily opinion-based by Jimmy Hoffa, GlenH7, MichaelT, MainMa, gnat May 23 at 5:44

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This is a good question, actually. I've been wondering this as well. reddit uses method 1, while imgur, for instance, uses method 2. Which is preferable and when? –  Shahar May 22 at 22:03
    
This is hardly opinion based. This is a question about separation of concerns-- it's like asking if MVC is a good idea or if you should just mash all your code together. –  RibaldEddie May 23 at 5:46
add comment

2 Answers 2

TL;DR

Both methods mentioned are not mutually exclusive. Either is fully capable of producing a web page for your users. If you need higher interactivity, you will have to add code to your page, using a framework or some combination with your own code. Your needs will determine which method or combination you require to build the web page.

No matter which of the methods used to create a web page you use, the rendering is done by the browser.

Server Page Generation

Some history

This may be old school (as of May 2014), but it is a tried and true method. The origins of this method started with Common Gateway Interface way back in 1993 (maybe earlier). Using CGI and perl scripts, a request for a web page could do something more than just feed a static HTML page to a browser. CGI has evolved into Java Server Pages, ASP.NET, PHP, and several other server based page generation technologies.

The Good

You can generate pretty much anything you want to send to the client. Pictures, documents, security encryption keys, etc. You can even generate HTML pages with dynamic content using Javascript on the page. There are some systems that don't store HTML pages anymore, but build the page on the fly (DotNetNuke is one example). It is possible to build a Single Page Application (SPA) this way.

The Bad

It is easy to fall into the trap that you have to make round trips to the sever to process every change on a page. You can dramatically increase the load on your server by poor implementation, although this is true of both methods.

The Ugly

Trying to mix Server Page Generation and SPAs can be a nightmare to build and debug. You have some code that is built on the server, but executes on the browser. A little planning will go a long way to mitigate this issue. Code reviews are also a good idea. If your junior developer can't follow what you are doing, your code might be overly complex.

RESTful Interface To A Server

Some (more) history

This is a relatively new technique (less than 5 years old). It is one method to make a web page more "reactive" to user interaction. Using AJAX as a transport mechanism between the browser and the server, along with some code on the web page to process the data, the user gets a web page that acts more like a desktop application.

The Good

This method of page generation allows for frameworks to be used in the construction of your interactive web page. The frameworks have advantages of being open source, heavily developed, heavily tested, and (maybe) documented. This method is not mutually exclusive with Server Page Generation. You will find that an application may appear to be fully SPA, but isn't (e.g., Google's GMail).

The Bad

Javascript not functioning on a browser. All of the browser variations that you have to support. You should have a static HTML page as a fallback for when the browser doesn't support your fancy framework (think text based browser, especially for the blind).

The Ugly

This is the same as for Server Page Generation.

share|improve this answer
add comment

The reality is that a modern web application (not a web "page") has a lot of moving, interactive parts. That means there's going to be some Javascript, and perhaps a lot of it.

This means that if you go the first route, you will be "complecting" the Javascript front-end code with the back-end code.

If you go the second route, you will be separating your concerns and making it possible for the back-end and front-end to vary independently.

As a general rule about software architectures, in fact, the second route is preferable. Loose coupling and high cohesion are more easily achieved by separating your concerns properly, which is what the newer design methodology will provide.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.