Using Redis to communicate between PHP and socket.io/node.js - php

I have a PHP app built and running on Apache, using Nginx as a reverse proxy to serve static resources.
I also have Redis installed which I am using to store activity ID's for each users activity stream. The activity gets written to a MySQL database, then Redis pushes the activity ID into each users stream. When a user receives his/her activity stream, the app first retrieves the users list of activity ID's from Redis and then gets the actual activity data via a MySQL IN() query.
This all works very well, however I want to start add real time capability to this set up. I would like to be able to push these events straight to a users browser and also add general live notifications.
For this I have installed node.js with socket.io. I have the socket.io server correctly up and running and a client is automatically connected on page load.
Where I am struggling is in understanding how to post a message to socket.io from within my PHP app. Since PHP and node.js cannot communicate directly, my understanding is that I would be best off utilizing Redis as a go-between since I already have it installed and up and running. However I have no idea how to go about this.
What I need is a description of the process (any code examples would be very helpful) of sending a notification from PHP to Redis and then into socket.io in order for it to be pushed to the relevant client.
Also, I do not understand how socket.io would know which client to send to. How would I pass this information along and keep everything synced? Is this even necessary? Do I need to store my PHP sessions in Redis and have socket.io collect the data when a user connects? Or is there another way?
Thanks in advance.
Please Note: My PHP SESSION data is currently saved to disk.

You can set up a pubsub channel on Redis (please see http://redis.io/topics/pubsub). Then subscribe to it from your node.js process. Your PHP would then publish to pubsub to communicate to the node. You can have separate pubsub channels for different clients and publish to whatever channels you need to reach your specific clients.

Redis doesn't offer inbuilt websocket or http server so we have to integrate it with php or node.js in order to stream channel data. with Tweak method, we can connect Redis server with php using predis php library for redis, where php will push data into Redis and socket.io will keep track of new messages pushed into Redis sever and beam it back to users connected to it in real time.
https://github.com/u-day/tweak/

Related

PHP websocket realtime updates on multiple devices

I am trying to implement a very simple self-hosted (docker) pubsub client update system using websockets or SSE for my PHP application.
I would like that a specific javascript action on my website triggers a php server-side update (via ajax) which should then lead to an update to all subscribed devices for the particular channel/topic. The subscribed devices should use javascript listening to one channel each.
Do you know any simple PHP ws libraries which are capable of that? Can SSE be triggered in that way and also utilize channels? How about in-built PHP socket function? Ideally I would like to stick to PHP solution in order not to deal with translations and in-between REST APIs.
After researching this for days and going through solutions like RabbitMQ, Redis, Kafka, RatchetPHP, ZeroMQ, socket.io, RPC, SSE and etc I am completely lost. I need some guidance on which way to go.
Among all possible solutions here is how I would do it:
Install a web socket server, preferably implemented in PHP, on you Linux system.
Your web clients register with the server and listen for messages like broadcast from the server.
Your ajax script registers also with the server and sends a broadcast to the server if all work is done.
The server passes the broadcast to all registered clients.
You could use my implementation of such a web socket server that comes with all the needed client
libraries for JavaScript as well as for PHP that implement broadcast and feedback.
See https://github.com/napengam/phpWebSocketServer

Send commands from a server to swift app

I am new to swift and was wondering how could I send commands from a server to my iOS application ? For example imagine a website where I can click a button and that button would then trigger a pop up to all users of my app. Is this possible and if so can someone lead me in the right direction?
Much appreciated
Yes, you can
Silent Push Notification: Send a Silent Push Notification and handle in your app. But you need user permissions to receive Notifications. Add "content-available" : 1 to your notification content.
Using Sockets: There are many options to create connection b/w device and server. example: https://socket.io/
It depends what you are trying to do, you might want to look at apple push notifications and see how they work. From there I would suggest looking into sending notifications to the apple server (that would send the push notifications to the app). I hope this sends you in the right path good luck!
I just assume that you have an application that a number of users are using and you have a web application that can show all mobile users.
For the most of cases, whenever a user is using the application, you let the application send a request with your user's information to your server and your server records the user information in the database.
When your website wants to show all the users' information, it just fetches all the information from the database instead of sending sort of commands to all applications to get information in real-time.
Of course, there is a more complex way to send commands to all running applications to get the results. However, it requires both server-side and client-side (ios application) implement functions sending commands in real-time using Socket or MQTT etc. This is more complex and it requires iOS application running in front or background at the same time.
I assume that using HTTP to record every mobile user is more suitable for your case.

How to implement push notification in cordova with angularjs using php json array

I have a project were I am required to add push notification for Cordova AngularJS Push Notification using PHP on server side which gives JSON array as output. I can implement chat etc using this, but I want to know how to implement it in push notification or accessing native app API. Firstly I thought using Javascript SetInterval, but it slow downs the app, and checks the API everytime...
For Cordova push notification you can take a look here:
http://phonegappro.com/tutorials/apache-cordova-phonegap-push-notification-tutorial-part-1/
Still, let's review some other methods of sending data to the client using Web APIs:
Long polling - keep the connection open on the server side with no or long timeout, and return a result when you want to send a push notification.
Use WebSockets - open a steady TCP connection to the server and transmit messages in both directions (unlike HTTP's request/response model).
Use PushManager - this is an experimental technology that is not yet supported on mobile devices, so I guess this is a no-go for you.
Use simple polling - every now and then poll the server for a new message.
Long polling and Web Sockets will overload the server if you'll have a lot of concurrently open connections, so I wouldn't go there. Also Web sockets are mostly used for communicating between clients (server passes the client info to both clients, then they can communicate on their own without the sever intermediating between them).
PushManager is too new a technology not yet supported in mobile.
For what you want (mobile I guess, as you want to use Cordova), and without knowing what you are actually trying to achieve, I would say go with #4. I understand you already tried it, but perhaps try to lower the polling rate to every 30 seconds or so.
I understand that while debugging it doesn't seem nice to send something from the server then wait the better part of a 30 seconds period until it appears on the client, but if you think of it from the user's view point you'll see that the user doesn't know when the server sends the data, so it does appear immediate.
However, if you're writing a chat client, then I would go with web-sockets, using the server to connect the two (or more) clients in the chat and letting them pass the messages directly. If you want the chat to be server backed, just periodically send the transcript to the server using simple AJAX.
DIY vs Hosted Realtime Data Stream Network Service
If you do not expect to have more than a few thousand clients using your app, the DIY (do it yourself) sockets technologies (WebSockets, Socket.io, etc) is feasible. Beyond a few thousand (many of our customers say in the range of 5 to 10 thousand) you will experience difficulty (and large expense on server resources and scalable code) scaling your service.
Using a hosted realtime data stream network service like PubNub, Pusher, Ably, etc will be less costly and complex and it will just work. With some of the hosted services (PubNub for sure - which I work for - full disclosure) provides the ability to publish the message in realtime and include a push payload (for GCM, APNS and MPNS) that will also send the push notification if the app is in the background or not running.
With PubNub BLOCKS, you can also implement server side code that runs in the PubNub Network to inspect, manipulate or process the message without having your server do the work. This means you could send the message to other third party services: translate message to another language, use AI services for whatever reason, send an SMS/email/etc and much much more.

How to keep my android app hearing for updates?

Hello there and happy new year 2013!!
I returned to my app after vacation, and now I want it to keep alive and hearing for updates!! Does anyone knows how to do so? I don't mean a complete answer, I mean like guidence, orientation!! Because I don't know how to start doing it!! And on the other hand, how do I send the updates from my PHP/PostgreSQL server? Has anyone did something like this before that could help me?
Thanks in advance!!
If you want to PUSH updates to your application, you can do so using GCM: http://developer.android.com/google/gcm/index.html
If you can simply have your application POLL for updates periodically, then you can use AlarmManager and an IntentService to have your application hit your PHP endpoint as needed.
The choice of which to use usually comes down to how timely the updates need to be and how often they are likely to occur (taking into account battery, network, and other resource usage and so on).
To get started with GCM using PHP check out: Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL
And, here's a good example of AlarmManager/IntentService (and handling wake locks).
My advice is to use GCM to send push messages to the device the app is running on, simply because the GCM service is always running and it will use less battery than polling.
As a side note: donĀ“t send the actual data via push notifications, instead just send a notification that there is new data on the server. The app can then download the new data from the server.
If by update you mean and update in version that is a user has version 1 of your app but there is version 2 available then you could do it like this :
You could have a variable that would save the current app version, then every time the user starts the app you should connect to your server to check the most up to date version. You then can compare and show an alert if the user has a lower version than the one you currently have in your server.
Two important things here : Every time you update your app you should look for the value of the version variable and change it and you should have the current app version variable in your server updated as well
If you mean updates in the data then your best bet is to use the GCM service (Google Cloud Messaging)

Get live data from a queue of ActiveMQ

I have to fetch live data from a queue of ActiveMQ installed on a remote server.
I am planning on using AJAX to get the live data. The following link describes how to do it
http://activemq.apache.org/ajax.html
But when I insert the described commands on the necessary location, I am not able to fetch the data.
I also checked the following links to learn how to get data from server to PHP script, but in vain ...
http://activemq.apache.org/using-apache-activemq.html
My Question is, how to I link my existing PHP page to fetch data from server and display it using AJAX ?
ActiveMQ AJAX support is an optional feature, as is the STOMP protocol, which are the two most promising methods for getting PHP to integrate with ACtiveMQ.
You should talk to the folks managing the ActiveMQ server and work out which mechanism they want used to get access to the system.
If they do want STOMP, a more messaging oriented protocol, there is reasonable PHP support: http://php.net/manual/en/book.stomp.php

Categories