Using SocketCluster with Koa

Andreas Urbanski
Aug 22, 2017 · 2 min read
SocketCluster and Koa, what a nice couple!

If you want to build a realtime application one of the main design choices you face is which backend to use. SocketCluster provides a NodeJS framework to build horizontally scalable WebSocket based realtime backends. And it rocks! Just look at this example to see how easy it is to get started:

npm i -g socketcluster
socketcluster create myapp

This will install SocketCluster globally and create the necessary boilerplate for you application. Neat, huh?

If you’re one of those people (like me) who want to have everything your way and your way only you generally don’t like boilerplates as is. You want to dig in and tweak stuff to your liking. You want to choose which components to use even if it didn’t affect the end result too much. Somehow it gives you that nice feeling that everything is in order.

With this in mind I wasn’t happy with SocketCluster using ExpressJS instance as the HTTP request handler. I like KoaJS more due to its minimalism and use of async/await.

By default SC worker node is initialized as follows:

To use Koa we need to pass the function returned by app.callback() as our request handler:

This leaves one question: How to deal with routes? We can use koa-router but we need to write our own healthcheck middleware to support Koa with koa-router. The default worker uses sc-framework-health-check plugin and a Koa equivalent could look something like this:

Add this to healthcheck.js and then in worker.js use as follows:

Now we can configure our routes like in a normal KoaJS app by passing the router object to some route initiator.

What about static serve? We want to be able to serve the client libraries to our client but if we call GET /socketcluster.js our router will take over and return 404 Not Found . To deal with this we can use koa-static , a great middleware that allows us to easily serve static directories.

There’s one problem however: if we mount koa-static in front of router the static middleware will now take over and /health-check will in turn no longer work. One way to go around this is to only mount our static middleware to the index route at / .

To do this we can use koa-mount which allows us to mount another Koa instance as middleware to our main Koa instance. Really cool!

Here’s an example middleware we could use:

Add this to static.js . Our final worker will then look like this:

And there you have it! SocketCluster worker with KoaJS, healthchecks, custom routes and static serve.

)

Andreas Urbanski

Written by

Sharing my thoughts on AI and data science, critical thinking and philosophy. Solving intelligence is my endgame.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade