Of pipes and feeds
I’ve been hearing a lot of good things about RethinkDB and its ReQL query language for a long time, but somehow I never tried it. RethinkDB is in a crowded space of JSON databases and the current king is MongoDB. Its not perfect but I am very pleased working with MongoDB and like its query language. The usefulness of a technology comes from its ecosystem and the MongoDB ecosystem is huge. No matter how good folks at RethinkDB are they simply can’t compete with the behemoth of all MongoDB users, library authors, hosting companies and authors of books and tutorials.
So in order for new technology to succeed in the marketplace they needed according to Geoffrey Moore a pragmatist in pain. The problem of writing real time web applications is the one that in January 27 ’15 they’ve addressed with the new feature called change time feeds.
Before that beside custom in house solutions, writing real time web applications was limited to choosing between firebase platform, which is service only solution something that alienates everybody who prefers to host on their own server and meteor. Meteor is great, if you are writing a real time application from scratch definitely you should consider it, since it offers so many things for end to end development, such as mongo database on both the client and the server, latency compensation, security model, code sharing etc. So I’ve decided to try how much of those goodies I could reproduce with available technologies and least amount of effort.
For the client I’ve chosen ReactJS, as my view layer. I’ve used Koa, just because it is small, though you could replace it with express or whatever since it only serves static files. The real-time communication was done using socket.io and I’ve used RxJS to orchestrate the whole thing.
First I’ve installed RethinkDB and played for a few hours with ReQL language. The installation went perfectly and working with ReQL was great, I especially liked the ReQL web administration page. It has autocomplete, realtime updates and it looks very nice.
Since I didn’t wanted to meddle with less I put the full css file made by meteor, & created a webpack setup to compile and bundle the jsx files. I decided to just start the meteor example and start porting the generated html code into React components, I only looked at Blaze templates in couple of occasions. Creating the React components was very easy, copy paste, add closing tags to inputs and change class to className. Hooking up socket io was also very easy. Beside the logic in the components RxJS is doing the heavy lifting and it does its job marvelously. Webpack made the code sharing a breeze and Ramda helped with low level functional tasks.
The problems were mostly in three areas:
1st RethinkDB doesn’t work on windows so I had to use Ubuntu virtual machine, and programming under virtual environment is annoying at best. Even on powerful server the experience was very poor.
2nd I had lot of problems with the React router. Either I don’t understand it or it doesn’t fit my model of thinking. I almost started to write my own router, luckily I avoided the temptation and learned to live with the ugliness.
3rd My lack of experience with RxJS especially on the server. RxJS is very powerful but it requires specific way of thinking. I still haven’t trained my brain to think in streams in a satisfactory way. I feel that I’m abusing BehaviourSubject where other approach might be better. I decided to use Async for the database setup since that is what I know and I was impatient to dig in RxJS literature.
The source of the end result could be found here. It still lacks polish but its usable. I’m pleasantly surprised how much real time functionality could you achieve with so little effort. RethinkDB change feeds work like charms, and seeing your application updates in real time looked very nice. Hope to see that windows port released as soon as possible.