Laravel REST Api as Server Push over HTTP2 - php

I am currently using laravel as my backend with a API built for android client
thats consumes the api.
I have configured websockets with PUSHER to handle new changes on server to be pushed to subscribed clients to avoid a server polling.
But sometimes the client need to go to server to get initial data from multiple endpoints or more than one image resources.
I have found the two libraries for laravel that help in implementing web side of laravel HTTP2 complaint
1 -> https://github.com/tomschlick/laravel-http2-server-push
2 -> https://github.com/JacobBennett/laravel-HTTP2ServerPush
from this link https://laravel-news.com/http2-server-push-middleware
So the question is it possible to implement HTTP2 on REST API endpoint where the client need to hit multiple url subsequently.
One solution can to inlucde all the resources in a single response, but that can be done only if all the responses are JSON.
How can I push json along with some small images.

Related

Interaction between service and remote api using websockets

I'm trying to create service which will interact between main service and remote api using http and websockets.
I need to send data from main platform to API and than react to API incoming events imediantly.
Remote API has two parts: Http and WS. I understand how to interact with http part but don't clearly understand about ws.
My main platform, service and sdk wrote by PHP. Also for service I use Redis as DB for save data of incoming events and their statuses.
I think I can setup WS server to send data through it to remote API. But I don't see all picture.
scheme that I imagine
How I imagine a hypothetical scenario:
Core platform - create event
My service handle this event, save data to Redis to check and for example change status,
format data and send it through my SDK to WS server
WS server has WebSocket connection with remote API. It publish my data to this stream
Remote API get this data and return response
Than, WS server is waiting incoming events from remote API
If server receive incoming data, it send it to my service (or create event which my service will catch?)
When service got incoming data it handle it and create event which main platform can handle
Am I right or it's wrong approach? Welcome to any help and tools titles :)

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.

Write data back to my backend server from a packaged Sencha-Touch 2 App

I am trying to write a Sencha Touch application that I can package in PhoneGap for iOS, Android etc...
The app allows you to input some data which I would like to save to my backend server using PHP/MySQL. It all works when I use an AJAX proxy as I am testing the app on the same server as my database.
However when I package it for mobile devices I run into the same-domain restrictions of AJAX calls. So I modified it to use the JSONP proxy and I can read the data from the server database but have no idea how to write records back. Do I need to somehow pass the data in the GET request? Obviously the server side PHP needs to be different because of the callbacks and the fact JSONP is forced to used GET requests. I am also trying to get CORS working but currently no success.
So my question is how would I implement writing data back to my backend server from a packaged mobile Sencha app running on a mobile device? Either using JSONP if that is even possible or any other alternatives are welcome?
Many Thanks…

whats the best way to implement real time push messages with apache, php and mysql only?

i'm trying to implement a chat application for android (where users can communicate people who are in same location). i'm thinking it of an facebook messenger with Geo specific oriented. i came across to terms like repeated pull, comet, bosh, websockects, socket programming, xmpp(which requires xmpp capable server like openfire) etc to do this. But my resources are fixed like apache, php (codeignter) and MySql only. I need to find the efficient to way to do this. Guys i need this. Please help me.
Thank you,
abbiya
The preferred approach on Android is Google Cloud Messaging.
Google Cloud Messaging for Android (GCM) is a service that allows you
to send data from your server to your users' Android-powered device.
This could be a lightweight message telling your app there is new data
to be fetched from the server (for instance, a movie uploaded by a
friend), or it could be a message containing up to 4kb of payload data
(so apps like instant messaging can consume the message directly).
The GCM service handles all aspects of queueing of messages and
delivery to the target Android application running on the target
device. GCM is completely free no matter how big your messaging needs
are, and there are no quotas.
Essentially, it works like this: Your server talks to Google, and Google pushes the message in real-time (or nearly so, as long as the device is powered on and connected to the Internet) to the Android device(s). There's a registration process that happens on the Android device the first time the app is installed (and at future points to revalidate the registration). In your Android app, it's your responsibility to send this registration ID to your server so you can store it for future use. Your server uses this registration ID when it wants to push a message to that device.
The communication protocol between your server and Google is JSON over HTTPS. Your server can use any languages/technologies, as long as it can communicate with Google's servers. A PHP/MySQL application can definitely meet the basic requirements for the service:
Before you can write client Android applications that use the GCM
feature, you must have an application server that meets the following
criteria:
Able to communicate with your client.
Able to fire off HTTPS requests to the GCM server.
Able to handle requests and resend then as needed, using exponential back-off.
Able to store the API key and client registration IDs.
On the client side, the Android device must be running API version 8 (Android 2.2) or later. Of course, it's also your Android app's responsibility to process incoming messages (using a BroadcastReceiver, as the Android system sends an Intent to your receiver whenever a message arrives), which allows you to handle messages however you want.

How to pass twilio sms through server websocket to client side

I am trying to setup my server so that I can have someone contact me through a chatbox on our website. Since I won't be there at all times to monitor the chats, I am trying to setup a system where the chat is routed through a websocket to my server (using PHP-Websockets) which is then sent to my phone via sms. This is all working correctly. I am having trouble wrapping my head around how my response can get back to the client side. When I respond to a text, Twilio will access any page that I specify with the sms message as a post variable. How can I pass this post message to my websocket (which is already running) where it can then send the message back to the client? Thanks for your help guys
Twilio communicates with your application via a WebHook call (HTTP request) when it receives your SMS.
Since PHP-WebSockets is running as a standalone process, and not as part of an existing web stack (e.g. Apache) then you'll need to have a few components in your setup:
PHP-WebSocket
Web server - serving up your HTML, CSS etc and also interacted with by the Twilio WebHook
A way for 1. and 2. to interact. This is usually achieved through some sort of message queue
Here's a sequence diagram that details the communication between the various components in your setup, as I see it:
Hopefully that clarifies the communication from Twilio, your web server, your WebSocket server and your client.
If this seems overly complicated then you could remove the requirement to have the WebSocket server and message queue by using a hosted service such as Pusher, who I work for. If that is the case then this using Pusher & Twilio tutorial should be useful. One of the main benefits here is that you can continue to think in terms of request and response, rather than having to think about cross component communications through message queues.

Categories