CSS-Only Chat - Kevin Kuchta

CSS-Only Chat

So, it turns out that css background-images don’t get loaded until the relevant selector is triggered.

Many people might say “neat!”

I used it to build a bi-directional CSS-only async chat.

For some background, it’s not hard to build a multi-directional web-based chat. It’s practically the “Hello World” of Node.JS. A bit of JS on the frontend, a bit of logic on the backend and you’re off to the races.

This thing I built does that with no frontend javascript whatsoever. Just html and javascript. The trick turns out to be abusing the http protocol and some fun properties of CSS.

A full technical writeup and a quick gif-demo can be found on github.

Building this was an interesting experience for me. It started (like most of my favorite projects do) as a “what if…”. Someone retweeed this davywtf tweet on using CSS pseudoselectors to send data to a server from a page with javascript disabled. That got me thinking: if it’s possible to send nearly-arbitrary data like that, you should be able to build something like a full chat out of it. Once I came up with a way to send data back to the frontend (using long-running http requests), it was clear it was possible.

I was (and still am, as of this writing) taking the summer off to travel after leaving my last gig (we got acquired, I wasn’t wild about the acquirer). As a result, I had this idea while in a position to spend as much time as I wanted on it. It turned out I needed a couple afternoons in a Paris cafe to get the core pieces in place and then another afternoon polishing + writing it up. I waited until morning, US time, and tweeted about it.

I was unprepared for the level of response I’d get.

I’ve always been pretty bad at predicting how popular the things I create will be. My AWS markov chain went nowhere; My blockchain comic went mildly viral. My wikivoyage explorer has interested exactly no one, but my ruby js nonsense has done pretty well (even becoming a RubyConf talk). Who knows what will stick when I throw it against the wall?

Anyway, this chat abomination became a wild success. Top of hacker news for a good chunk of the day; tripling my twitter follower count; 5k stars on github. Old acquaintances reached out when they recognized my name and friends told me when coworkers posted it in their work slacks.

I credit the success of this to:

The moderately-popular things I’ve built before have tended to die out after a day or two in the sun. Even the blockchain comic petered out after something like 3 days. This CSS thing went on for a solid couple of weeks. I’d be watching BSG on my ipad weeks later and I’d have to silence twitter notifications so that new follower notifications don’t keep interrupting me.

I’m now writing this three months after the fact and I think I can official call it “over.” The last twitter notification I got for someone liking that tweet was 5 days ago. I figured it was time to finally write about it a bit.

Looking back on it, I’ll definitely admit that the attention was fun. Maybe I’ll try to relive it at some point by turning it into a conference talk.

The best aspect of all this, though, is that I now have 3 moderately-successful hacks (ruby-as-js, totes-not-aws, and this). That constitutes a pattern. I’m starting to become known as “that guy who does the horrible/clever things.” I’m pretty happy about that. Maybe that trend will continue.