Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

I’ve been working with background geolocation with an ionic application. There’s a cool plugin to do that. The free version of the plugin works fine, but here’s also a premium version with improvements, especially in battery consumption with Android devices.

Basically, this plugin performs a POST request to the server with the GPS data. When I was developing my application, I needed a simple HTTP server to see the POST requests. Later, I’ll code the backend to handle those requests. I can develop a simple Silex application with a POST route and log the request in a file or flush those request to the console. This would have been easy, but as far as I’m a big fan of WebSockets (yes, I must admit that I want to use WebSockets everywhere), I had one idea in my mind. The idea was to create a simple HTTP server to handle my GPS POST requests but instead of logging the request I will emit a WebSocket. Then I can create one site that connects to the WebSocket server and register on screen the POST request. OK, today I’m a bit lazy to fight with the frontend, so my log will be on the browser’s console.

To build the application I’ll reuse one of my projects in GitHub: The PHP dumper. The idea is almost the same. I’ll create a simple HTTP server with Silex with two routes. One to handle POST requests (the GPS ones) and another GET to allow me to connect to the WebSocket

That’s the server. Silex, a bit of Twig, another bit of Guzzle and that’s all!

And each time background geolocation plugin POSTs GPS data Silex POST route will emit a WebSocket to the desired channel. Our WebSocket client just logs the GPS data using console.log. Is hard to explain but very simple process.