I was missing MySpace's "online now" badge so I built my own
I'm Online Now
You may have noticed my homepage now features an image which sometimes has a basic border around it, and other times it has a blinking orange border around it. The orange border is meant to represent that I'm online now.
By online now I mean that I am at my desk sitting at my computer. I'm never at my computer unless I am working on something, personal or professional, but it could also mean that I'm in a meeting or available for a chat.
I've shown this to a few people before but since I had some questions about it I thought it would be helpful to have a write up on how I built everything I needed to get this to work.
NOTE: Everything after this is going to cover the separate pieces of code I wrote so if that does not interest you, you can stop reading here.
Table of Contents
Server
I don't use PHP a lot but here's what I came up with. When this part is called the following happens:
- Check if the token is set
- Check if it's a
PUTrequest - Check if the token matches my "secret token"
- Read and parse
status.json, update thelastSeenkey with the current time, and save that back.
Client
I've been using xbar (formerly bitbar) for a few years, even contributing to a few plugins myself. Its a nice way to run a script (many languages supported) and have that output display in the macOS menu bar. Everything is pretty boilerplate to how an xbar plugin works and the interesting part happens in the last few lines.
Because of the way macOS works, my computer wakes every so often and does whatever network requests it does in the background. xbar runs in the background too even when the screen is locked and off and because I wanted to actually only mark myself online if I'm on my computer, I needed to handle this case a different way. I found a few different answers but they did not work for me as they did not return what I thought to be the correct values.
The following snippet actually returns the idleTime in minutes so while it could be useful for this case, it ultimately wasn't as clean as my chosen solution.
My chosen solution was actually to check the DevicePowerState and if its ON, mark myself online:
Web
The final part was to get that value and use it somewhere on my site. I don't really like adding javascript unless absolutely necessary and I couldn't really find a way around it for this case. Since I was already going to use javascript, I figured I might as well build it as a Web Component.
And then I can add the following to where I want it displayed and things should start working.
As Web Components go, it doesn't have a lot going on but I like that its all selfcontained to this block of code. The OnlineNow component does the following:
- Loads the status from the
hrefdefined in the tag - If valid, check if
lastSeenwas in the past 5 minutes - Set the
valueattribute withonlineif it is, offline otherwise - Recheck every 1 minute
I have some basic styling in online-now.css but I also added some additional styling to my h-card so that is visually different when online: