Creating a real time website using PHP - php

I'm currently creating a website using PHP and the Kohana framework. I want to site to be able to use real time (or near real time) data (e.g. for chat and real time feeds). I need it to be able to scale to thousands of concurrent users. I've done a lot of reading and still have no idea what the best method is for this.
Does anyone have any experience with StreamHub? Is it possible to use this with PHP?
Am I digging myself into a hole here and need to switch languages? I've looked at node js and nowjs, but I'm weary about coding a while site in Express (I wonder about security holes, code maintainability, lack of a good ORM). I've read about Twisted Python, but have no idea what web framework would work well on top of that, and I'd prefer not to use Nevow - maybe Django can be used well with Twisted Python? I'm just looking to be pointed in the right direction, so I don't go too far in PHP and realize I can't get the near real-time results that I need.
Thanks for the help.

I've looked at node js and nowjs, but
I'm weary about coding a while site in
Express (I wonder about security
holes, code maintainability, lack of a
good ORM).
I can personally vouch for code maintainability if you can do JavaScript. I personally find JavaScript more maintainable then PHP but that's probably due to lack of PHP experience.
ORM is not an issue as node.js favours document based databases. Document based databases and JSON go hand in hand, I find couch db and it's map/reduce system easy to use and it feels natural with json.
In terms of security holes, yes a node.js server is young and there may be holes. These are un avoidable. There are currently no known exploits and I would say it's not much more vulnerable
then IIS/apache/nginx until someone points a big flaw.
I want to site to be able to use real
time (or near real time) data (e.g.
for chat and real time feeds). I need
it to be able to scale to thousands of
concurrent users.
Scalability like that requires non-blocking IO. This requires a non-blocking IO server likes nginx or node.js (Yes blocking IO could work but you need so much more hardware).
Personally I would advice using node.js over PHP as it's easier to write non blocking IO in node. You can do it in PHP but you have to make all the right design and architecture decisions. I doubt there are any truly async non-blocking PHP frameworks.
Python's twisted / Ruby's EventMachine together with nginx, can work but I have no expertise with those. At least with node you can't accidentally call a blocking library or make use of the native blocking libraries since JavaScript has no native IO.

PHP is not the language you should be using for real-time updates of a website. PHP scripts load first before HTML (and HTML calls javascript files), so PHP cannot update your page for you. However, when used with AJAX (eg. using a jQuery function to call a PHP file to update your page in real-time), you can use PHP in this fashion.
Using jQuery and AJAX (all javascript), you can do quite a bit in terms of updating a page without reloading it. I've seen sites such as this one that demonstrate how to make a chat using jQuery.

Related

Websockets & PHP

I'm starting to consider websockets as a solution to replace long polling in a new build PHP app I am commissioning.
I have a few questions which I wonder if people could help me out with.
Can a Nodejs server call PHP and if it did wouldn't it suffer the same shortcomings as just going through Apache in terms of the connections? We all know nodejs is non blocking and Apache etc isn't but if Nodejs is just making a call to a PHP server in it's own procedure would that not bottle neck in a similar way?
Are PHP and websockets a good match?
Are there any good js libraries besides socketio which apparently only works with Nodejs?
Has anyone found a good tutorial which uses websockets and a PHP backend maybe using something like that Ratchet PHP library which might help me get on my way?
Thoughts would be muchly appreciated.
Please excuse my paraphrasing of your questions.
1: Can Node.js call PHP, and wouldn't that have the same shortcomings as Apache?
Calling a run-once PHP script will have the same general shortcomings as calling a web page, except that you are removing an extra layer of processing. Apache or any web server itself is such a thin layer that, while you'll save some time, the savings will be insignificant.
If PHP is more effective at gathering data for your clients than Node.js, for whatever reason, then it might be wise to include PHP in your application.
2: Are PHP and WebSockets a good match?
Traditional PHP scripts are normally intended to be run once per request. The vast majority of PHP developers are unfamiliar with event driven development, and PHP itself does not (yet) have support for asynchronous processing.
PHP is a fast, mature scripting language that is only getting faster, even with all of its many warts and shortcomings. (Some say that its weak typing is a shortcoming. Others say that it's a shortcoming that its typing isn't weak enough.)
That said, the minimum that any language needs in order to implement WebSockets is the ability to open up a basic TCP port and listen for requests. For PHP, it is implemented as a thin wrapper around the C sockets library, and there are additional extensions and frameworks available that can also change the feel of working in TCP sockets with PHP.
PHP's garbage collector has also matured. Memory leaks come either from gross disregard for the memory space (I'm looking at you, Zend Framework) or from intentional sabotage of the garbage collection system by developers who think they're clever or want to prove how easy it is to defeat the GC. (Spoiler: It's easy in every language, if you know the details!)
It is quite possible and very easy to set up a daemon (long running background process) in PHP. It's even possible to make it well behaved enough to gracefully restart and hand its connections off to a new version of the same script, or even the same script on the same server running different versions of PHP, though this is treading out of scope just a tiny little bit.
As for whether it's a good match, that is completely up to the developer. Are you willing, able, and happy to work with PHP to write a WebSockets server, or to use one of the existing servers? Yes? Then you're a good match for PHP and WebSockets.
3: JS Libraries for WebSockets
I honestly haven't researched them.
4: Tutorials for using PHP and Websockets
I'm personally fond of this tutorial: http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html
Although I have it on good authority that the specifics of that tutorial will soon be obsolete for that specific WebSockets server. (There will still be an actively maintained legacy branch for that server, though.)
In case of link rot:
Using the PHP-Websockets server (available on Github, will be homed soon), extend the base WebSocketServer abstract class and implement the abstract methods process(), connected(), and closed().
There's much better information at the link above, though, so follow it as long as the link exists.
It would hit the same bottleneck if you go through apache. This can be remedied by using a different web server, such as lighthttpd or nginx. You won't even need node at all.
PHP does not have decent shared memory making the biggest advantages of a WebSockets irrelevent. It should be decent enough if you don't want interaction between users, but even then I would have to frown upon the usage of PHP. PHP is great for a lot of things, but real-time communication is not one of them.
You might want to look at https://github.com/einaros/ws.
PHP is not a good back-end. Anything with an execution model that isn't run-and-forget in its own sandbox, such as Node, .NET, C/C++ and Java are good matches. PHP is suited for short running executions, such as actual web sites and even web services -- but not real time connections.

Fool-Proof Group Chat in PHP

I am trying to implement group chat (only group chat) functionality using Ajax, PHP, MySql.
What I have tried till now:
1- Ajax short polling with caching to reduce number of database hits. But it is rejected by number of developer's because Number http request's.
2- Ajax long polling, which is quite good idea but it is not working with IE even if I made every ajax request unique.
So please give me some solution so that I will be able to implement group chat functionality in my project. I am open to new suggestions to implement group chat.
A group chat on short polling would be extremely inefficient. It's not true real-time (a few seconds of delay unless you want to hit the db every single instance then your gonna hurt the system)
Now with ajax (short polling or long polling) it could a solution but not an effective choice especially when talking about scalability!
A newish technology node.js would be the perfect solution. If you don't know what node is, it's fairly simple. Think a non-blocking io javascript on the server. Now this would require a learning curve if you are not already familiar with javascript but i assume you are. Second is that this is a php question combined with cakephp which node.js replaces completely.
Now node.js provides many many frameworks that are similar to Sinatra, Rails, and some other php alternatives. From a simple Sinatra based framework to a full stacked mvc framework node has it all.
I recently been extremely interested in Express, which is a sinatra type framework. It provides it's own view templating system "Jade" but you can use any other engine you wish. The site gives a few screencasts showing a few example applications and some tutorials showing the power of node.js and Express. You can write detailed and advanced applications in an extremely short time span since both the front-end and back-end is written in one language (javascript). It also provides a nice html alternative mark-up.
Though Express is only one of many frameworks, i'm sure there is one you would be interested in. You can find a stackoverflow question here.
I did find a chat application written in node.js available on github though it's not in active development any more, it's still a good resource to look at and see how other people have implemented their chat systems.
Another aspect of node is the database tool. You can actively make queries in node utilizing the none-blocking io architecture.
There are plenty of ways but the more efficient is the question. A note to be made is that you need a server that supports node.js, some new cloud services are specializing in node. A simple google search might find some good ones.
Hope this helps,
Daniel
EDIT: I also forgot to mention that to get a fully real-time chat you can use socket.io. Though the real reason to use node is because it's meant to be used in an event driven non-blocking io way. It also has an extremely small footprint, so scaling is not an issue.

What are the relative advantages of various Python/PHP web frameworks (particularly for my project)?

I'm deciding on a web framework for an upcoming project, and I'd appreciate any advice. We've decided to use jQuery for the JavaScript, and are heavily leaning toward Python or PHP (more Python) for our server-side logic. I'm especially interested in web2py because of its jQuery integration.
About our project
Our project is to develop a security console for a complex
cybersecurity system operating within an organization's internal
network.
This console will be largely server-driven, as messages come in from the network and must be pushed by the server to the user.
The user will also be able to initiate security actions, the implementation for which will likely be in C++.
The interface we've planned will be relatively rich, and I want to leverage jQuery's power as much as possible.
We have some control over the browser environment we'll be running in (e.g., we don't have to worry about clients with JavaScript disabled).
Our site is likely to have only a few, long-lived client connections.
We are looking for software components with permissive licenses, though we're using some copyleft components (I see that web2py is LGPL while Django is BSD, so +1 to Django)
We have about a month to create a functional demo of our system, of which this interface is a small (but visible) part.
About us
We are two developers with about 5 years of programming experience, but little web development experience. I have several years of Python experience and a summers' worth of experience messing around with PHP. My coworker has some Python experience and has never touched PHP.
I used Django once back in 2008, and was frustrated by the file and code structure, which I found highly unintuitive. Perhaps this structure is inherent to the MVC model (I've had similar experiences with Django and CakePHP since), and I just need to bite the bullet and memorize it.
My Question
Given the information above, what are the relative advantages of the various Python/PHP web frameworks for our project? As mentioned above, I'm especially interested in web2py because of its jQuery integration, though Django's dominance is (once again) hard to ignore.
Thank you very much for your time!
Before deciding on a framework, you should first decide if you want to commit to a language you are unfamiliar with.
You said you've both got minimal PHP experience, so you have to weigh up the advantages here; Will the pros for going PHP (if any) out weigh the amount of time the developers will need to spend to retrain?
(Although depending on your background experience, PHP should be very easy to pick up.)
If you frame it like that, PHP would have to have a pretty convincing offering to give you. From what I'm seeing, specifically Django vs web2py, they both seem very close in functionality - which is good, but doesn't provide the "you must use x!" scenario you may be after.
However, If you will be bringing more people in later and feel finding people to work with web2py will be difficult, it may tip it to PHP. You said your self, Django's popularity (and BSD license) is hard to ignore, and it should make it easier to find people for later expansion.
If it were me, in your shoes, I would go with web2py. Assuming the development team will continue to be Python focused for the foreseeable future.
Python vs PHP: Python
With python, you can always write wrappers for C code so you won't have to mess with starting other processes and passing args to them. That might be useful for your security functions.
Web2py will allow you to easily write a webservice for this too, to more easily integrate the C portions with the web-site infrastructure.
If you already prefer python, I would go with that. If you need to bring on web-developers later that are trained in PHP, teach them Python. It won't take long, and I'm sure they'll appreciate it in the long run. Plus, moving from a PHP MVC framework to web2py or even django would make things easier. I've used CodeIgniter for PHP and find that web2py was so much simpler and easy to understand.
Also as for the directory structure, django is not actually true MVC -- it's MTV (model, template, view).
I find web2py's organization a little more straight-forward. But yes, either way it can seem strange at first. I would say YES, you should bite the bullet and use MVC.
In web2py, the "view" is html markup with the ability to write raw python code. The controller extracts data from the model (database), attaches any needed files (css/js etc) and the model of course simply defines the structure of the data and allows you to access it in an OO way.
Lastly, I wouldn't tip my hat in favor of web2py just because of jQuery integration. It does use it, and a some of the built-in framework stuff (like response.flash/session.flash, the LOAD function that loads a page or data from another controller) rely on jQuery, but using it in another framework only means you have to write an include statement (e.g. ).
But, the way it allows/forces you to focus on development is what takes the cake for me.
I've been using Django as part of my work for a couple years now and truly enjoy it when I can make it work. Unfortunately, and maybe it's just me, but I end up spending hours working on configuration every time I start a new server, or try to make it work in a development IDE.
It's relatively simple to start a new project and start coding. But there are all sorts of little catches that keep things from working if you deviate from the norm. Things like if you want your django project to serve from a subdirectory like example.com/django. The information is out there to make it work. But it's hard to find.
I can't tell you if web2py has those same problems or not. I only just learned about it from your question. It does look slick and simple though. I can tell you that after the hassles of getting the applications to serve properly from whatever server configuration I'm using, django is way more fun to program with than plain php. PHP frameworks may differ.
From my understanding the project not usual web application, you must need event driven program, web server in python.
Django won't help you here.You should look into Flask, Flask has inbuilt console too.http://werkzeug.pocoo.org/, you might need to use twisted, gevent,Flask jquery.
I would recommend asking about web2py on its mailing list -- you'll get lots of help and advice there. Regarding the LGPL license, note that it only applies to the framework, not your application -- you can license your application however you like, including closed source/commercial.
It sounds like you'll need some server push functionality, but if you'll really only need a few long-lived connections at a time, you might not need to bother with solutions like Twisted or gevent. If necessary, though, web2py does include this (requires Tornado, and works using WebSockets, though can be adapted to use other methods).

Using PHP for medium to high-load public websites

I am planing to build a website using PHP to be hosted on the public internet with decent user load (between 1000 to 5000). I am using FreeBSD as a server and I configured Apache, PHP and MySQL from scratch.
With proper configuration, is it safe to use such a server, or should I go with some web framework? I am asking as I've heard few horrible things about PHP.
If it is safe, does PHP get too complex when the size of the site increases beyond a certain point?
I know Facebook uses PHP; are there any other big websites that use PHP?
Last, is it recommended to use some PHP framework or should I stick to plain old PHP?
PHP works fine for just about any size server. The question isn't really the programming language but the infrastructure you set up. 1000-5000 users is not very many unless they are all banging on the site at the same time. Are they doing a lot of DB queries or consuming a lot of CPU resources? If so, then you may want to look at a dedicated MySQL server for the DB queries.
I have nothing against frameworks. However, you are usually shoehorning your problem into their solution. Careful design on your part with common routines, etc., are usually just as good as a framework in my opinion. However, some people are more comfortable working within a framework because it removes some of the plumbing issues.
A lot of large sites use PHP. It may not be obvious because they hide the extension of the scripts in the URLs.
With proper configuration PHP is fine. However if youre new to php and server administration you might want to read up on configuring php as well as Apache for security. Youll also need to read specifics on configuration for performance as well but you need to have an app to test before its really worth doing that beyond some basics.
As far as using a framework or just basic PHP that depends totally on you. a framework is othing more really than a set of useful code and structure to accomplish alot of tasks. If you dont use a framework youre going to have to write code that meets similar requirements, so you might save time using a framework. Generally you are going to sacrifice performance in trade for having to design/write less code. You need to decide yourself if a framework (and which one) is right for the project.
In terms of sites that use php... There are a ton... Facebook, Yahoo Bookmarks, Daily Motion, parts of MySpace (at one time, not sure if its still the case), anything running Drupal or Wordpress... PHP is more than capable.
PHP is just a tool and choice of framework does not really makes your application secure or fast, unless you understand the principles of web security and how things works.
Facebook is good example of what php is capable of in hands of processionals. And there are plenty of websites that capable to handle 10K visitors a day on a single low end server.

Php/Javascript to make a browser game?

I've been on and off intrested in making a text based browser game.
I have been turned off by the idea because of the daunting amount of things to learn.
PHP (or another sever side scripting language)
Javascript
HTML
MySql
And the fact of severs and apache..
Can I just pay for web hosting and by-pass having to set-up apache?
Also how long will it take me to learn all thoose things well enough to start work on my game?
Should I just stick with Flash and then C# for XNA?
Just install XAMPP, which is basically the lazy man's Apache/PHP/MySQL setup in 1 click. You just install it and it does all the work, nothing for you to setup nor config.
Don't let the complexity of PHP/HTML/Javascript set you off, we all hard to start somewhere. Just start with the parts you know how to make, then look around for each individual problem. Being motivated is key to learning anything, and if you have something you enjoy working on, you won't have a problem learning what you need to pull this off.
Do you have a specific game in mind?
Does it need to be multiplayer?
In the initial stages, does it require server interaction at all?
Conversely,
Are you interested in the possibility of building a working game first, and adding in features like saving high scores, multiplayer, or other server-interactions later?
Is a self-contained game like nethack or Hitchhikers a good starting place for you?
If your initial game does not require server interaction, you can build quite complex games using only HTML and JavaScript. This will reduce the number of concepts and languages you need to learn up front.
Once you have had some practice building non-trivial games in HTML and JavaScript, you can then add in features like server-integration, and learn a server-side language like PHP, Python, Perl, ASP.NET, or Ruby...
You can definitely pay for hosting, and eliminate the effort of setting up and maintaining a server yourself. A quick search will find you a number of web hosting sites to choose from.
I believe sticking with flash would make things easier, as syncing multiplayer with javascript and PHP isnt going to be easy.
It depends how complex the game you want to make is. You can easily learn some basic PHP and javascript inside of a month (like pretty much any other language), but it'll take a lifetime to master (like any other language).
I would recommend you start out small - plan to implement just a subset of your features, and take them on one at a time.
I would plan to use libraries / frameworks. For Javascript I would currently recommend JQuery because I find it easy to use, it has a large community and it's well documented. Using a library like JQuery allows you to easily ignore a lot of the browser specific details, since they deal with all that nonsense for you.
For PHP I personally use Zend Framework - this is a massive beast that isn't always the best documented, but if you start with the "Quick Start" tutorial and then only look at features as you need them, you shouldn't get overwhelmed too easily. One of the great things about Zend is that you can pick and choose what features you want to use without hassle.
With regards to servers, yes you can use a web host and bypass setting up a full server yourself very easily and cheaply. You can find local installations (eg. XAMPP) that will allow you to quickly set up a local install of Apache, PHP and MySQL to get started with.
First of all, if you're developing an MMO or want to offer multiplayer support, you're going to need to learn PHP and mySQL. In theory, you could go ASP.NET instead, but I would strongly advise against it, since Microsoft servers cost more to rent, and since PHP/mySQL is much better documented and easier for beginners to learn. If you're building a single-player game, Javascript and HTML could technically be adequate, but knowing PHP will still make your life easier, in the long run.
Although you should probably set up a LAMP stack (Linux Apache mySQL PHP) on a local network so you understand how it works, hosting your own server almost never makes sense for a production environment. You could, however, save yourself some money by developing the game on your own LAMP stack. Alternately, most shared hosts (around $80 / year) provide support for PHP and mySQL, and this would be perfectly adequate for building your game. Eventually, once you're ready to launch -- and assuming it becomes even marginally popular -- you're going to need at least a VPS, and possibly a dedicated server.
Finally, a note on Flash:
In the last few years, the popularity of mobile devices has skyrocketed, and this presents a risk for Flash games, since Apple refuses to support the plugin. If you need advanced graphics support, a more timely alternative would be HTML5. Unfortunately, this carries its own set of drawbacks: namely, since it's a long way off from official release, it's not yet universally supported, and features that are supported will vary from one browser to another. So basically, HTML5 is the future, and Flash is the past. The best option for right now? Probably Javascript; you might be surprised what JS can achieve, when properly combined with CSS, and you'll need it for AJAX functions anyway. Best of all, it's supported by virtually every device and browser.
So, in conclusion, I'm recommending that you learn HTML, CSS, PHP, mySQL and Javascript, and don't be intimidated by that variety of languages; the more you learn, the more easily you learn the rest.

Categories