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.
Related
I am relatively new to node.js and socket.io. Currently I have a half finished private web project, which runs only with PHP with a MySQL database on the server side. I decided to bring it to a more advanced level using socket.io, for several features within the project.
So I read a lot about it and watched a whole bunch of tutorials. Also I found this and this during my research.
My question is, if that is still the common way to develop a web application?
More exactly: to use on one event (like a form submit) both an AJAX request and a socket.emit, for those events it is necessary/wanted.
The background of this thought is the following. I have a whole bunch of calculations running now in PHP. And the node.js server runs logically in JavaScript. So I can easily implement a node.js server without changing anything on my AJAX requests. Or rewrite everything I have so far, to js and use only a node.js server.
But this leads to 3 more questions:
Which runs possibly faster on the server side. A calculation scripted with PHP or JavaScript?
How to use transactions on a node.js server while using MySQL?
And how great is the influence by converting a PHP array to a JSON object, what you could avoid with the usage of just the node.js server where you don't need to convert anything.
JavaScript is executed on the client side so you are limited by the user's hardware whereas PHP is executed on your server. See this post for more info about performance comparaison.
I highly suggest you take a look at this pure node.js client that will perfectly do the job in your case.
PHP has many functions to use on JSON data (json_decode(), json_encode(), ...) but Node.js don't require JSON data to be converted. In the end, it really depend on your usage and how you plan to store and use that data
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 ..
I've developed an application that I would like to use meteor.js for real time updates (I want to enhance but not change my program, for example when a user adds a comments make it update in real-time ) . Problem is meteor.js uses node.js (so javascript as server-side code). I use LAMP stack, Is it possible to get PHP to feed data into meteor.js from mysql.
Meteor is more than just an 'interactive webapplication'-builder or javascript framework. The idea is to have only one programming language (besides HTML/CSS for markup) to do all the work. Basically it creates a 'remote server' (in the clients browser) it can push data to and at the same time it publishes various API's to the users system. The data passed through these API's / connections has a specific structure which has to be adhered at all time.
Meteor is built around NodeJS, which makes it hard (if not impossible) to run it without this backend. Sure you can try to mimic the backend using PHP, but it would be a waste of time. Reading your question you'll be better of using a javascript framework like jQuery or Prototype. Unlike Meteor you will need to do the AJAX calls (POST & CallBack) yourself, but you can actually decide which backend you want to use yourself (including PHP / MySQL).
If you want to do this anyway you need to check the Meteor & NodeJS source code to see what the minimum requirements are to make Meteor run under PHP. The PHP stack has to interpret the commands Meteor sends and receivers, but this won't be an easy task.
You can use comet (or reverse ajax) for realtime updates.
Trying to marry node.js with PHP doesn't sound like a worthwhile path to go down. If someone insisted on using a system like Meteor.js, yet with a PHP back-end, it would make more sense to look at AngularJS which is mainly the client side.
Of course, that is different technology stack. If someone really insisted on the blending, one could consider using server side sockets to interact with PHP Web services; and/or use mongodb and/or mysql-node to interact with the same databases.
I released a meteorite package that interacts with a Wordpress site that has the Wordpress JSON API. A quick fix. For now.
Comes with a backend call that will return the raw data, or a publication that stores the posts using their id's instead of a randomly generated mongoid. And some basic templates to get you started including a Session variable that keeps track of the currently selected post.
I'm working on it a lot more and will eventually have a version that directly makes mysql calls from node so you won't need php or Wordpress; just the ability to access the mysql database (which can be remote, with the appropriate configuration, or on the same machine).
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)