Events in PHP slim web services - php

I am writing a web service in PHP using the Slim framework. I have a web application and a mobile application which uses the web service. My web application posts some data to the web service and web service writes it to a database. But I also wanted the web service to send a notification to my mobile application.
I assume this is some kind of an event based action. How to perform this for PHP slim web services? Or in general, for a PHP web service?

I personally use the symfony2 EventDispatcher component to achieve what you speak of. It's a clean way to, well, implement an event-based architecture. This particular implementation of the mediator pattern can make your code extensible with minimal overhead (and much organization).
Link: http://symfony.com/doc/current/components/event_dispatcher/introduction.html
Note: The EventDispatcher can be used independently of symfony2. The above link contains the packagist link of the component.
Edit: If you are a fan of global variables, you could look into the do_action function in WordPress.

I think there is no easy way to implement a direct event notification from Slim to a mobile device. To do such thing you need to open a socket between device and server so you can send direct notification to the device.
With Slim, the easiest way to achieve what you want is to use the list/queue pattern and tell the device to periodically ping your web service (like every 30 sec) and check if there is something new in the queue.

Related

Mobile App - Communicating with an external server

I'm building a mobile project that needs to be in constant communication with a server and i need some information . I know how to build local apps but this is the first time that i'm building an app that requires external call to a server / authentication service .
I'm asking for guidance how to proceed and which services/servers to use.
What type of server/database do i need ?
I'm guessing i will be requiring an API service but no idea how to choose/make one.
I want to use azure services/database but i also don't want to be dependent on it. I want to have my own url that i send request to and interact with a server/db that i can later move to another host fairly easily.
I develop websites mostly and i'm familiar with php/laravel + Mysql but i think in this case it will be overkill to create a laravel app simply for the server backend .
The app will be mobile only so i don't expect to have a webpage for it . simply an external server/database where the data will be saved.
First i need an authentication service - where each user will register on the phone which will then be saved in my external server/database . Then when they need to login - they will input the login details on the phone , which will query the esternal server/database and if validated - get their details from the server.
First, you need to decide which kind of server communication you'll need. it's in real time and constant? then you'll need a websocket. It's in bursts when you need to send or get data? then you'll need some kind of webservice (REST, RPC, SOAP). Then you have to evaluate the user load you'll have. And finally, the human resources you'll have.
Based on your question, I think a REST webservice will be more than enough. You may:
-Create a REST service for every group of related resources. Example: the /user URL should handle the signup, login, logout and user update operations.
-Create a method for each one of those operations and handle them. Then, call the method from the REST service class.
-Depending on the amount of users and the technology you're using, create a server to handle the requests, or upload your REST project to a server (tomcat, for example).
-Create an app and consume the REST services from there.
There are tons of tech you can choose for these things. PHP allows creating REST services, I think. Java is a very good choice too, since you can use the same code in both server and android apps. Node.Js is pretty popular, too, since you don't need servers and uses NIO (althought java can do both things using jetty and also has multithreading); golang and scala both have superb performance (golang is a lot more easier to learn, though, and it has no need to use external webservers).
Hope this helps.
For mobile applications the best will be REST (representational state transfer),becouse is lightweight and flex to use in other technology. I had project which include REST and mobile app and web app and it working very well.
In this scenario, we usually will build a REST API service for client end. As you are familiar with Laravel, you can refer to http://www.programmableweb.com/news/how-to-build-restful-apis-using-php-and-laravel/how-to/2014/08/13#apiu for how to build a REST API service with Laravel. Also you can leverage other light 3rd part PHP frameworks to build REST API service.
You can create and develop the application on local and then deploy to Azure Web Apps. Please refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-web-php-get-started/ for more info.
And there several vendors provide MySQL services on Azure. ClearDB is a BaaS on Azure for MySQL. You also can use the VM to host your MySQL Service. E.G. MySQL by Bitnami and MySQL by Docker.

Web to Native app - Request & Response - Listening service

This is a very general question, however I have done some research and believe I am simply missing some useful terminology of the concepts in question, and if someone could point me in the right direction it would be helpful.
Basically I am creating a PHP page, that will send a request - this will then be received by a native app. Where the user will either accept or decline the request. This will then be sent back to the PHP page.
So my queries..
What technologies or key concepts need to be applied here..
Main areas
-- PHP to send request
-- Native app to be listening for requests
-- PHP to be able to listen for the response
-- All needs to happen in real time i.e. user requests service, administrator confirms/declines, user receives response. All within in est. 60 seconds.
If you want to go into the right direction and you want all this to be on PHP, I would look at PHP RESTful service. Maybe
read up on this resource: http://www.amazon.com/RESTful-PHP-Services-Technologies-Solutions-ebook/dp/B005VQ8SB6/ref=sr_1_1?s=books&ie=UTF8&qid=1419012976&sr=1-1&keywords=9781847195531
You can also look into Symphony or Zend frameworks, there are other frameworks that has similar feature. If you need this quick, you can get it installed and test out what you're looking to accomplish.
Also take a look at Symphony snippet on Request & Response - Listening service
http://php-and-symfony.matthiasnoback.nl/2011/10/symfony2-how-to-create-a-custom-response-using-an-event-listener/
Something on previous post from Stackoverflow
symfony provides a solid framework for HTTP and restful API through which jQuery mobile can be configured and then if you extend TWIG for a template, then Symfony acts as the bundler and composer through with routing can take place. While Symfony does not run native apps, they need to retrieve and store data and on that front a framework such as Symfony works well. So one could argue that one could use APIs for multiple clients (iOS, Android and more) and then reuse controllers and use formats to generate XML/JSON thus theoretically it should work with Phonegap too.
#ref: Creating mobile app in Symfony2

symfony working with Web Service

I am working in symfony. I am retrivind data from a web service.
Currently I am using "Listener" to get data using web service. Is it wring way to do through listeners?
Is there any concept of Models in symfony to get data using web service? rather than calling web service from listener!
Actually a listener isn't something that would fit your case. You need a regular service, because listeners are supposed to react to Symfony domain events.
On creating services, you should read the official documentation (though if you've created an EventListener then most parts of the manual would be familiar to you).
If you're wrapping a foreign API then you should definitely do some research on whether the API is already wrapped (packagist.org is a good start: here's a wrapper for Twitter's API for example). If it's not, then it's up to you to pick an HTTP client to communicate with the service, and wrap its API into a PHP class that you would then expose in your service.
To understand how exactly you want to do this, try searching Packagist for Symfony bundles that wrap some APIs. Here's a Foursquare API bundle that uses an abstracted client library for example. Note that it depends on Guzzle HTTP client, and also take a look at the Guzzle Client class.
Also, here's Google's official API client for PHP. You could grab some ideas from there, too.

Does drupal or joomla allow a combination node.js and socket.io with php?

I'm looking into the development of a site where socket use is a must for real time updates. I understand with wordpress it is difficult to incorporate node.js and socket.io and was wondering if this is the case with drupal. Specifically could I incorporate node.js and socket.io with php? What specific steps would I need to take? I appreciate the help and of course if you give me a good answer, I'll rate you up.
There are currently Node.js modules that connect to Drupal, Joomla, and WordPress. I wrote the one for Joomla: https://github.com/jlleblanc/nodejs-joomla You can find all of these through NPM. First, you would download one of these modules, then you would also download the Socket.io module. Then you would write server side code connecting the CMS to Socket.io. Finally, you would write client-side code to talk to the socket running in Node.
You can have it separated and since you want some client-server communication with node.js, you can have it - outside Drupal (this is the easiest and cleanest way).
CMS you use (be it Drupal or Wordpress) does not limit your JavaScript from using socket.io and node.js for the calls you want to be made outside the CMS server-side code. You can eg. read the same database Drupal/Wordpress does, but using Node.js and returning the results directly to the client-side JavaScript script.
You can integrate Socket.IO with whatever technology you like, but you need to think of them as 2 separate parts that need to communicate.
I see 2 solutions for Socket.IO to communicate with an external service (Drupal, Joomla, Wordpress, whatever):
1) Making a rest API for your Socket.IO application (for example using Express for the API layer and then passing messages to Socket.IO with an EventEmitter).
2) The better solution in my opinion is to use a message queue like Redis, RabbitMQ or ZeroMQ. The Drupal website would send a message down the channel to Socket.IO and then Socket.IO would send that message to the client(s).
If you are creating a system that needs authentication also, I suggest you don't let your users send message directly with Socket.IO, but make an Ajax call to your main server instead (Drupal for ex.). That way you can check the user's identity more easily. (This scenario would be a fit for a chat based system that requires authentication)
Although this video tutorial is for EventMachine with Faye, the same logic could apply to your app: http://railscasts.com/episodes/260-messaging-with-faye

Chat system in Zend Framework website

In my website, I have to make a chat system similar to Gmail so registered users can chat with their group. I have no idea about how I will do this. Can anyone provide me with an idea of how to implement this or any useful links? I have to do it using Zend Framework.
Since you are looking to work this out using Zend framework, have a look at Jaxl library (Jabber XMPP Client/Component Library). The library can be integrated with any existing website/framework and also contains several examples for browser based chat applications.
Actually there are two parts for chatting:
A long running request that streams massges from the server to the client
Ajax messages from the client to the server that sends one message to the server
For the second, any ajax framework will do it. For the first you might have a look at Comet to get the idea for this. But you should be aware that html isn't ment to be a chatting protocol. If you don't pay attention such stuff can easily kill your server.

Categories