Problem Statement – To build a two-way communication between Android (Client) and Php Codeignter (Server). I have to first send a message from Android to Php Server and Php Server internally calls another API. Now Android (client) should wait until some response from third-party to the server using webhooks. I have to send that webhook data back to Android (Client) using the interface without polling into my server repeatedly.
What is the best possible way to approach this?
Note: I tried the Mqtt protocol and I already have an API for this using polling. Just need to convert to a better way.
2
Answers
In case your CodeIgniter server is waiting for a response from a third-party via a webhook, and you don’t know when the answer will come, consider using notifications. The WebSocket connection from mobile does not have to stay active for as long as it takes to receive a final response.
Based on the type and amount of data in response, you can either send it through the notification itself or wait for it in the mobile app. Upon receiving a notification, you can then make a new request to your server for additional data.
There are 2 ways to go about it.
Recommended method: (Use a REST API to publish message to your websocket server)
You need to build a service that does that.
Or event better, use a ready-to-ship service like PieSocket realtime
They allow you to connect to the websocket from frontend/android and publish messages via PHP (CodeIgnite/Webhook)
Method 2: PHP WebSocket client
Subscribe to a websocket channel from your Android device and then use a PHP WebSocket client to publish messages to it and eliminate polling.
Hope it helps.