php socket programming,Pros and cons and Approach - php

I am working on a task where it is required to distribute live data (being fetched from some other server) to client using sockets. We user drupal as a framework and thus my choice of PHP.
1)I want to know the major factors that should be taken into consideration while developing this server.Like security,authentication,load etc and how should i approach this.Is there any blog/article that could be of help.
2)Is there a better choice than PHP for this?
3)ALSO is there any drupal module that could assist me in this.

I can think about one issue when you are talking about several servers connecting to you, you may want to conceder a nonblock mode.
Because when you are working with blocking mod, each server can connect at the time, meaning that there will be delay delivering the message.
http://il2.php.net/manual/en/function.socket-set-nonblock.php
I think java would be better choise, multi thread may help here.
from my point of view, this is something that you need to develop your self, socket programming needs a lot of attention.

Drupal is a Web CMS, it can be used as framework for advanced web features or light to medium web application that fits well in the traditional HTTP request paradigm. IMHO, distributing live data from multiple servers to client using sockets doesn't fit with the base assumption behind Drupal's design.
PHP can be used to write socket server. It used to be un-common, but it is becoming more and more available. ReactPHP is a non-blocking I/O library/framework suited for socket programing. Racthet is a websocket server in PHP using ReactPHP.
In any case, this would ne nothing like developing a web application with RoR, Drupal, Django, Symfony, etc.
If integration with a Drupal website is required, the Service module is a nice solution to provides a REST or XML-RPC API. Or course, direct access to Drupal's MySQL database is also an option but will probably require more knowledge of the used Drupal modules since you will have to replicate their behaviors and understand how they manage their data (for instance, how a particular CCK field is stored in your database).
Note: Previous version of this answer included reference to the following solutions in other languages: Twisted (Python), EventMachine (Ruby) and Node.js (JavaScript). These are all valid solutions when PHP is not required.
Previous version of the answer also referenced phpsocketdaemon, but nowadays ReactPHP is a more clean and robust solution.

Related

PHP - delegating tasks to executables

In the application I've written there is a whole class that only takes in some parameters, eventually fetches the db for other parameters, and then returns the results to the asking class. It only handles data, so it would be perfect to "outsurce" those functions to something more efficient than PHP.
I've read the official php faq page on "PHP and COM", and seen that it is not possible to run a precompiled dll from php.
So, do you think it is possible and worth to do it with some other methods? And how would you do it?
Maybe PHP/Java Bridge / RESTful WS could do, but still java does not sound like the best option (according to this post it's "clunky")
HipHop would be a great solution, but as of now it's way too early to use it in production for my needs.
All the solutions listed here seem to be aimed at creating an .exe file for making a standalone win app, whereas my app is hosted on a centOS server and I haven o need to port it to windows (or at least that's not my intention right now).
I use C++ and RabbitMQ to tie it together. I've also had success, when there wasn't much of a scope for the project, at using Redis as a tie in.
Can't say enough good things about using a message bus to separate processes (like RabbitMQ).
If you're just getting started with RabbitMQ, I'd highly recommend RabbitMQ in Action.
You don't have to use C++ though. That's one of the major benefits of using message buses; you can substitute later, if need be (and the API stays the same). Here are a list of clients that can receive and post messages to RabbitMQ: http://www.rabbitmq.com/devtools.html
Insert favorite, high-performance language here.

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.

Thoughts on converting PHP app to ROR for use with Heroku?

I'm in the process of building a rather big web application with PHP + Codeigniter. When I first began this project I was excited by the hosting provider PHPFog product of PHP-as-a-service. The idea of simply developing my application and not having to worry about server maintenance, setup, securing, etc appealed to me.
However, I've had far too many issues with PHPFog to be comfortable trusting it with hosting my application. I've run into situations where I've deleted a file from my git repository, pushed it to my remote repo at phpfog, and wound up with the file not actually being removed on one or all of my application servers. The service is also supposed to provide newrelic for application monitoring however this only worked briefly and hasn't worked at all since August 10th despite numerous complaints. And their customer service is far from satisfactory in helping solve all of these problems.
So now I'm considering alternatives, and Heroku has caught my attention. Heroku seems like a much more mature cloud application platform. However it does not provide PHP hosting. Instead it provides ROR, Java, Node.js, and Clojure.
How difficult would it be for someone with a lot of experience in PHP (and the Codeigniter framework) to learn Ruby + Rails and rebuild an application? Both organize code in the MVC pattern, so I hope that means my views would only require modification of their hooks to match ruby's syntax. I've already designed my database and all of the SQL queries to access the data I need from my models in CI. What do you guys think?
EDIT 1:
So I've watched this video as an introduction to ROR development:
http://www.youtube.com/watch?v=Gzj723LkRJY
And my initial reaction is 'So ROR is like a coloring book?' I'm skeptical when I see huge chunks of an application come together via something as simple 'scaffolding.' I don't know what to think other than I'm afraid that ROR sacrifices some of the granularity/control I'm used to with php
EDIT 2:
I've recently discovered https://cloudcontrol.com/ They appear to offer the same type of hosting with PHP-as-a-service that PHPFog offers but with more control, such as the ability to directly access your database and auto-scaling. Still the great idea of a git-push to deploy to multiple servers without having to deal with setting them up manually. The only thing I dislike is that their datacenters are based in Ireland (Amazon AWS). However they told me that they're planning on moving to the US in the next 3 months and offering pricing in USD.
While yes they both are MVC, yes both Ruby and PHP are scripting languages, and yes you shouldn't require much modification to your views other than changing the php hooks to ruby hooks, I think you are fooling yourself if you think it is just an easy conversion.
Ruby as a language is IMO far superior to PHP. It allows you to do so much more with so much less code. If you were to convert your PHP code to Ruby code by replacing each call with its equivalent, you wouldn't be doing it the Ruby way.
On top of that, Rails as a framework is far more mature and powerful than CodeIgniter. It will provide you far greater flexibility and convention-based help that you will code things a lot differently than if you were using CI.
Added to that, you will want to use ActiveRecord as your ORM and should write database migrations to create your database, so all those SQL scripts you have written will be pretty much useless.
If you decide that porting your app to RoR is the way to go, then I wholeheartedly encourage you to take some time and learn Ruby and Rails, and then rewrite your application as if you were doing a Rails app from scratch. You'll be amazed at how quickly you can get a project up and running.
Before this question turns into the typical Ruby vs PHP discussion, and before you embark on an (almost) impossible task, you should consider other hosting providers. There are many to choose from, some of whom offer this "PHP as a service" you're looking for.
http://vps.net/
http://mediatemple.net/
http://rackspace.com/
Please realize that porting a PHP application to ROR or Ruby is not an easy task. You may as well start again (in a language you know little about, no doubt). You shouldn't base your programming language on bad hosting experience or whatever that guy said. Use what you're comfortable with.
This one is a must read for you
http://www.oreillynet.com/ruby/blog/2007/09/7_reasons_i_switched_back_to_p_1.html

Web technologies for an embedded server

I've recently started a new web development project for an embedded device and wanted to solicit some recommendations for technologies to use. The device will serve HTML pages which include AJAX code to retrieve data from a JSON server. We're tentatively using Cherokee as the web server, though we're not tied to it.
Currently, I'm considering the following technologies:
Write it all in PHP. I know it's big, slow, and bloated, but I've got about 10MB available for the web interface (a lot for an embedded system), and we won't be seeing a lot of traffic on any of these devices. It does need to seem responsive for the users, however (pages should load in less than a second).
FastCGI + a C program - We're using an in-memory database, so the C program could interact with the database directly through the API. This would have much better performance than PHP, but development time and reliability is a concern since C isn't very well-suited for web development.
Lua + Kepler - This seems like a nice middle ground between performance and development time. However, I've never worked with Lua, so I'm not really sure how to implement it in an embedded web project. I'm also uncertain as to how well it integrates with the Cherokee web server.
So any opinions or past experiences with the above stated technologies? Any others I should include in the list?
Thanks,
Alex
When I was in this area, I used Lua and a simple FastCGI runner (Luaetta [for I'm sure the latest source would be available if you asked the guy] , though I'm also sure that's not the only one, and there's Kepler of course), spawned by lighttpd.
It performed quite well on an embedded media player, and was used for remotely accessing content and controlling the device. Though I don't maintain it anymore, you can find more about it at http://matthewwild.co.uk/projects/wooble . If you think the source would help just poke me for it, it's currently only available via a package manager but I can fix that given the motivation.
Another (again Lua) project in this area is LuCI. These guys are dedicated to making a web interface for embedded devices (routers specifically), and have produced a nice framework with lots of supporting libraries geared towards that kind of system.
I wouldn't be concerned with not knowing Lua. If you know any language then you can pick up Lua in a day or two, the manual documents the whole language and is quite short.
How about looking at HipHop, Facebook's PHP compiler?
https://github.com/facebook/hiphop-php/wiki
That way you can write your code in PHP and effectively compile it to C++.
ASP.NET. Assuming that you wouldn't be interested in Embedded Windows Server 2008, you could still leverage ASP.NET by incorporating Mono into Cherokee. You could leverage Visual Studio as your RAD development environment and use things like ASP.NET MVC 2. A lot of third party user controls will also 'just work' with Mono (Telerik Announces Support for their ASP.NET controls on Mono!).

Old desktop programmer wants to create S+S project

I have an idea for a product that I want to be web-based. But because I live in a part of the world where the internet is not always available, there needs to be a client desktop component that is available for when the internet is down. Also, I have been a SQL programmer, a desktop application programmer using dBase, VB and Pascal, and I have created simple websites using HTML and website creation tools, such as Frontpage.
So from my research, I think I have the following options; PHP, Ruby on Rails, Python or .NET for the programming side. MySQL for the DB. And Apache, or possibly IIS, for the webserver.
I will probably start with a local ISP provider for the cloud servce. But then maybe move to something more "robust" and universal in the future, ie. Amazon, or Azure, or something along that line.
My question then is this. What would you recommend for something like this? I'm sure that I have not listed all of the possibilities, but the ones I have researched and thought of.
Thanks everyone,
Craig
If you want a 'desktop component' that is available for you to do development on whenever your internet is out, you could really choose any of those technologies. You can always have a local server (like apache) running on your machine, as well as a local sql database, though if your database contains a large amount of data you may need to scale it down.
Ruby on Rails may be the easiest for you to get started with, though, since it comes packaged with WEBrick (a ruby library that provides HTTP services), and SQLite, a lightweight SQL database management system. Ruby on Rails is configured by default to use these.
The languages you list are all serverside components. The big question is whether you can sensibly build a thick client - effectively you could develop a multi-tier application where the webserver sits on the client and uses a webservice as a datafeed if/when its available but the solution is not very portable.
You could build a purely ajax driven website in javascript then deploy it to the client as signed javascripts on the local filesystem (they need to be signed to get around the restriction that javscripts can only connect back to the server where they served from normally).
Another approach would be to use Google Gears - but that would be a single browser solution.
C.
If you wan't to run a version of the server on desktops, your best options would be Python, Rails, or Java servlets, all of which can be easily packaged into small self contained servers with no dependencies.
My recommendation for the desktop would be HTML 5 local storage. The standard hasn't been finalized, but there is experimental support in Google Chrome. If you can force your users to use a specific browser version, you should be OK, until it is finalized.
I would recommend looking at Django and Rails before any other framework. They have different design philosophies, so one of them might be better suited for your application. Another framework to consider is Grails, which is essentially a clone of Rails in the groovy language.

Categories