So, that's a pretty weird title. How does someone use a Social Network to organise their Microservices?
To explain, let's jump back a few days.
I was having a chat to friend at work who was telling me about his project where he's set up a camera attached to his Raspberry Pi that watches his room while he's not there. However, he's not hooked it up so that that it turns off when he's home. The idea is to turn off the camera when his phone connected to the Wi-Fi - a pretty good indication of if you're home or not. Cool idea.
So, this got me thinking about how to turn this scenario into microservices. Well, the obvious breakdown would be to have a Network Poller Service that checks the network every so often for an IP address. It could then send a request to the Camera service to turn it off. Jobs done.
I was bored, and decided to see if I could create a similar network poller that scans my network at home, and then sends me a Direct Message on twitter every time someone connects or disconnects (you can check out the source code here).
However, this got me thinking; how do I alert twitter when the polling is finished? Do I just send the direct message right away? Or can I extract out this message process? And for that matter, can I make this process event driven?
So the idea of my architecture would look like this: Network Poller (send Post request stating finished with data) -> Twitter Service -> Camera Service (if I had one).
That makes sense. But wait, now our Network poller needs to know about both the Twitter Service, and the Camera Service. What happens if I add in another service?
This is where the event driven stuff happens; I need both the Twitter Service and the Camera Service to be listening to the Network Poller to make this work. So I actually need a 4th service hidden in here: Network Poller -> Organiser -> Twitter Service -> Camera Service
Ok, so this new Organiser service should handle the change request the Network Poller sends, as well as then passing this information to the Twitter Service and Camera Service (who can then request the Network Poller Service if necessary for more info).
Now, I've got to build me an Organiser. Wouldn't it be great if a service already had streams, with a well-known API, and loads of different languages already supported? We do; Twitter.
In this process, we can actually use a bespoke Twitter Feed as the message stream for all of the services.
Network Poller posts a tweet onto the feed which is "#NP #updated" The Twitter Service is watching this, and when it gets this message it will ask the Network Poller for updated information, which it can then Direct Message me stating someone has either joined or left the network.
The Camera Service can ignore this, as it doesn't care about this tweet. But it could care about
"#NP #updated #master"
Which could mean that I'm home, and would then turn off the camera. Using certain hash tags or a certain string, you could send commands to different services on your network to do things.
My current thoughts for this format (I've yet to implement this in practise) are as such:
#ServiceIdentifier #verb #focus
Where "focus" is the focus of the message.
This would allow for multiple services to be looking out for a particular Tweets, without different services needing to know other services exist; they only care about the Twitter feed.
There are some pretty obvious draw backs to this idea.
To start with, you're relying on Twitter, a 3rd party company which you don't have control. Also, you're limited to 140 characters per message. Each message can't get too complex. If your apps are particularly chatty, you've got to watch out for usage limit too. Don't want to spam Twitters servers!
So, what do you think? Could it work? If you've got any thoughts or comments, go ahead and tweet me @Hazz223