I want to create a web application. I do a lot of java and am not overly impressed with the web application frameworks available.
I was thinking about using another server side language like php but I don't want the learning curve.
This brings me to javascript. Can and should I perform all of the server side calls with ajax? I have a service oriented architecture available and can access all the data I need.
Is it more advantageous to use a server side language in conjunction with javascript to build a robust web application? Is it preferable to use pure javascript and make ajax calls to my web services?
You can, but the question is should you? It's been recommended to fallback gracefully for the people with JavaScript disabled since the beginning, but some big services and sites don't seem to mind anymore. You can't open Facebook, for example, without JavaScript enabled.
That isn't to say you should do it. You've got to justify it by making the experience even better. Make sure its navigation works perfectly and you utilize the history correctly.
A client side application also has to deal with browser differences on a whole new level. This is of course made easier with frameworks such as jQuery, but you will notice the difference between the browsers.
Can and should I perform all of the server side calls with ajax?
Yes. you can perform all of the server side calls with AJAX. Even now a days a lot of single page apps are emerging which make heavy use of AJAX and Javascript MVC to communicate with server. Even some of the javascript frameworks provide REST support inbuilt(e.g. - Backbone JS). But having said that, you should consider having fallback mechanisms for AJAX calls. So, if user has turned off the javascript he should be navigated to appropriate page. (Again this depends on your choice of whether to support nonJS or not)
Is it more advantageous to use a server side language in conjunction with javascript to build a robust web application?
The advantages for using javscript depend on your test case and mainly the audience for your service. But it is definitely more "adventurous" to move towards asynchronous communication, but only when you use it in a structured way. I suggest to go with any of the MVC framework on Javascript side for this.
Is it preferable to use pure javascript and make ajax calls to my web services?
Again preferance depends on the use case scenario of your web service. There is no harm in using javascript as such. The only thing is you should be little aware about security of the javascript calls you are making.
When you build a web application solely with Javascript, you are giving entirely all loads and processes to the client. Well technically the server still does some work, since you're going to use ajax calls, yet performance-wise considering low-end PCs or average mobile devices, they'll be having a hard time loading up your web app..
In the case of using PHP and Javascript together, you are given such flexibility to give your web app more performance, since the client and server are sharing loads and processes with each other.. And of course using PHP will give you better tools to access your site's database easily, MySQL in this case ..
Related
I'm having a web application build in PHP and I would like to add some real-time functionality to it. What I actually need is to control jut some parts (notifications "elements") of an existing page with nodejs. Would that be possible? I don't want to have to rewrite all the page on node.js.
I'm trying to find out if node.js could be the best approach for my needs (I never worked with it) or if it's better to use ajax long polling.
I read about using PHP and nodejs together, and about building one upon the other but I didn't found an answer about the situation I described before.
Node.js is a server side application plattform, so you cant use it in the Browser directly.
If you want to add realtime functionality to your site, you first have to think about the transport architecture. Either you use ajax with polling or you use websockets (e. g. with socket.io). In both cases you will need a serverside application to handle this.
This could be either PHP or Node or any other server side thing.
I know the basics of both PHP and Node.js, but I don't understand why some people argue over which one is better... Can Node.js be used to render web pages like PHP can? For example, can you make a BBS using Node.js (I know you have access to DBs, but rwndering posts seems to be a problem)? It seems to me like Node.js can be used either as a very simple HTTP server, that only serves basic HTML, without changing it, or for communication (which I use it for). For example, I'm making a browser MMO game, and I use PHP to serve the site and the forum/devblog, and Node.js for the actual game. Or am I missing something?
Can Node.js be used to render web pages like PHP can?
Yes. There are some differences, but the main idea is to use Node.js on the server-side to serve responses to the requests, similarly to PHP. So again, yes, you can render web pages using Node.js.
Can you make a Bulletin Board System using Node.js?
Yes. Rendering posts also can be done within Node.js. You can render posts also on client side JavaScript, so why you think server-side JavaScript (Node.js) would be more limited? For templating see eg. {{ moustache }} or Pure.
The main advantage of Node.js over "standard" PHP is that JavaScript is event-driven and you use single thread for all the requests, whereas in PHP you use separate thread for every request.
Node.js lets you write your own "server" using only JavaScript. It simplifies a lot if you want to build eg. communication server.
More resources
For more comprehensive solution for rendering web pages in Node.js see eg. ExpressJS Node.js framework. You may specifically be interested in the way ExpressJS allows you to render views.
Someone will give a more detailed answer than me but basically node.js can replace both Apache and Php. It's a platform that allows you to create a web application using only javascript. It has a different model respect to apache because node is event driven (being written in javascript) while Apache uses threads.
Anyway take a look here for a decent tutorial on node, the best way to understand it's by using it i think
Node.js and PHP can both be used to build server-side applications, but they have different strengths and weaknesses. While Node.js excels at handling large amounts of I/O and real-time applications, PHP is often used for traditional web applications. They can be used together to complement each other's strengths.
I'm setting up a realtime app that will be using socket.io. There's currently some core functionally in php, that utilizes memcache and mysql backend.
Would it make sense in the socket.io server to do an ajax request (if that's even possible) to the php page that handles this? There's a lot of MySQL querying, I know it can be done in node.js, but I'd rather keep this part abstracted in php if possible.
So, my question is, is that a proper thing to do? Call a php page from within the socket.io server to then return to the client?
Thanks!
I don't see any problems with having your node.js app communicate with your PHP app by exposing a RESTful API or some PHP script that you can POST to or GET from your socket.io node.js server. There are plenty of npm modules (like request) that can make HTTP requests like that a breeze for you. After retrieving the data from PHP in your node app, you can use socket.io to emit() the data to the socket.io client on the frontend.
There is nothing wrong with that. You are simply using a RESTful API to access the MySQL data, thus isolating the database details.
If one day you are tired of PHP, you can easily switch to Ruby, Python or Whatever for that part without even touching the node.js. If your logic is already written in PHP (you are upgrading an old app), it make even more sense as you can reuse what has already been tested and debugged. A lot of folks are advocating for that kind of separation between systems. Just look at all the SOA (Service Oriented Architecture) buzz.
Where I work we are using this very architecture in a project (though in this case its an ASP.NET MVC Website calling a Java EE app) and it served us very well. With the event model of node.js, its even better since you won't block waiting for the PHP.
But of course, there are some drawback
Performance overhead
Architecture is more complicated
You now work with two language instead of only one (though javascript and PHP are so often used together that I don't think it's really is a problem in this case)
So you need to ask yourself if your problem really need that solution. But in a lot of case the answer may be yes. Just don't forget the virtue of Keeping It Simple and Stupid (the KISS principle)
I'd like to develop a near real time web based chat system. Any suggestions on how to implement this via jQuery, any gotchas to look out for, and what is this Comet thing I keep reading about?
Ideally, I'd like to support up to about 5,000 concurrent chatters.
Comet, also known as Ajax Push, is often refered as "Reverse AJAX". Instead of pulling the information from the server in regular intervals, the data is pushed from the server to the browser when it is needed. That requires an open connection, for which there are several implementations.
I recommend that you use APE. Here is a demo: http://www.ape-project.org/demos/1/ape-real-time-chat.html
Advantage: It will be very responsive
and real-time.
Disadvantage: You need
to setup the APE server on your
webserver machine.
Comet is a "push" tecnology, created to avoiding the client (javascript code) to continously poll the server. This could cause bandwith problem, because you have to create (maybe) a new TCP connection, then contact the http server, he runs some server-side logic and then sends a response to the client. With comet, if the server decide that you should recive some information (e.g., new chat message) he directly send it to the client.
There are several different implementation, you can have a start here.
the simplest implementation tecnique is the hidden iframe, but I'd raccomend the long polling wich is much more controllable.
One more thing, thake a look at HTML5 websokets, wich could be an interesting solution to your problem (not very compatible with current browser, anyway)
Check out Node.js and nowjs for node.js. Node.js helps you build very efficient servers using server side JavaScript and nowjs is a library that allows you to build real time web apps. There is even a example screen cast that puts together a basic chat application in 12 lines of code.
You could also checkout Socket.io which is another node library thats helps you build real time apps by abstracting away different transport mechanisms and giving you a unified interface to code against (supports WebSockets, Flash Sockets, AJAX long polling, JSONP Polling and Forever IFrames).
I realize you tagged your question PHP but if you are seriously considering writing a scalable system with the least amount of effort (relatively speaking) then learning Node.js is worth your time (and the learning curve is not thats steep since you probably already know JS).
I want to create a live, checkers-like app, which will work like this: There will be multiple icons/avatars displayed on this checkerboard like surface. I want to have a command prompt beneath this board, or some other sort of interface, that will allow them to control a certain avatar, and get it to preform actions. Multiple users will be using it at one time, and I will all be able to view the other user's changes/actions to the checkerboard.
What I'm wondering is: what's the best way to do this? I've got my HTML, CSS, and JS approach down, but not my data storage method. I know that, using PHP, I've got the choices to use either: file-based storage, MYSQL, or some other method. I need to know which is better, because I don't want to have server-lag, poor-response time, or some other issue, especially in this case since actions will be preformed every other second 2 or so, by these multiple users.
I've done similar stuff before, but I'm wanting to hear how others would handle it (advice, etc.) from more experienced programmers.
Sounds like a great project for node.js!
To clarify, node.js is a server-side implementation of javascript. What you'll want is a comet based application (a web-based client application that receives server side pushes instead of the client constantly polling the server), which is exactly what node.js is good at.
Traditional ajax calls for your clients to poll the server for data. This creates enormous overhead for both the client and the server. Allowing the server to push requests directly to the client without the client repeatedly asking solves the overhead issue and creates a more responsive interface. This is accomplished by holding asynchronous client connections on the server and only returning when the server has something to respond with. Once the server responds with data, another connection is immediately created and held by the server again until data is ready to be sent.
You may be able to accomplish the same thing with PHP, but I'm not that familiar with PHP and Comet type applications.
Number of users and hosting costs will play into your file vs DB options. If you're planning on more than a couple of users, I'd stick to the database. There are some NoSQL options available out there, but in my experience MySQL is much faster and more reliable than those options.
Good luck with your project!
http://en.wikipedia.org/wiki/Comet_%28programming%29
http://www.nodejs.org/
http://zenmachine.wordpress.com/2010/01/31/node-js-and-comet/
http://socket.io/ - abstracts away the communication layer with your clients based on their capability (LongPolling, WebSockets, etc.)
MySQL and XCache !!!!
Make sure you use predefined statements so MySQL does not need to compile the SQL again. Also memtables could be used to use memory storage
Of course make use of indexes appropriately.
If the 'gamestate' is not that important you can even store everything in XCache.
Remember that XCache does not store data persistently (after Apache restart)