headless websites – intro

To coin a coined phrase, headless website.

Headless Website – Whats the big deal

I haven’t found a good way to describe this web architecture. There is the repository pattern that starts this idea off.

It looks like this:

Database (MySQL) –> Private (or Public) Api – (PHP) – “The Backend” –> Middleware Api / Restful Api –> Frontend – (AngularJS)

Best Practices REST API from Scratch – Introduction

This structure introduces the decoupling of code for better control, great accessibility and a robust api interface. Now the api is able to be used for accessing data for the web app and the iOS app. Now you can spin up more front end servers to handle the load with out having to as many backend servers (possibly dedicated) running allowing for better monitoring, stability and deployment.

Continue reading headless websites – intro

Redis Pub/Sub PHP and Node.js

Redis’ pub/sub is a super simple way of communicating events between parts of applications or completely separate applications.

This turned out to be a great solution for a project that had a background php worker that was generating calculations but had a front-end web socket in node.js that was pushing those out to the client.

A simple timer on the node.js was the intermediate solution. The issue was the timer in node.js verse the cron tab run would never match up and would take an extra cycle in the timer to get the data so the live data was still aways behind.

Pub/Sub was the solution since both apps were already using Redis to store and retrieve the data (along with other resources on their own). But there was a problem. The message part of the Publish(Key,Message) can only be a string. The data that was being sent was already complex and couldn’t be appended naturally with out a lot of code. So instead of trying to add to the array that was being stored as json originally, I took the data and added it into a stdClass object that contained the other info that I needed.

<?php $key = 'importantInfo1';
$message = new \stdClass();
$message->who = 'client1';
$message->what = 'thing2';
$message->payload = $arrayOfData;

$redis->publish($key, json_encode($message));

This allowed for me to identify the data without have to inject it in the original array and remove it later.

On the node.js side, I found another “issue” but a quick google search solved it. (http://stackoverflow.com/questions/7330496/redis-node-js-2-clients-1-pub-sub-causing-issues-with-writes). The issue is a node.js redis connection can be a getter/setter and a pub/sub connection at the same time. I don’t know the reason but the solution is easy. Make 2 connections, name one “redisDbGetSet” and the other “redisDbPubSub”.

There is also another step before you can do the “on” step for receiving those messages.
The connection has to subscribe, which makes sense due to what its doing and so a standard connection isn’t flooded with messages it doesnt want.

redisDbPubSub.subscribe('importantInfo1', 'importantInfo2');

redisDbPubSub.on("message", function (channel, message) {
        case 'importantInfo1':
        case 'importantInfo2':

Continue reading Redis Pub/Sub PHP and Node.js