Fly + WebSockets + Google Pub Sub article

Hey y’all!

I’m working on an article about using WS + PubSub with Fly.

Here’s the repo of the demo app: GitHub - PierBover/fly-web-socket-google-cloud-pub-sub

Haven’t deployed it on Fly yet, but you can check the code.

The app is a Fastify Node.js server that roughly works this way:

  • When the server starts, it checks whether a PubSub particular topic exists. If not, the topic is created.
  • Each VM of the Fly app will have its own subscription. This ensures each PubSub message sent to the topic is received by all VMs.
  • When the VM is shut down, the subscription is deleted. If there was a problem and this didn’t happen, the subscription would be deleted by Google anyway after a couple of days of no one acknowledging the messages.

When the server starts, it starts sending a message to the PubSub topic at random intervals with the server ID (max 10 seconds). This message is received by all subscribers and sent to all the WS connected clients.

The front end is served by this server too. It connects to the WS server, and displays the WS messages received:

Now for the article, this is roughly the content I have in mind:

  • Introduction to Fly
  • How WebSockets work in Fly
  • Strategies for integrating PubSub with Fly
    • Ensuring a message is sent once to a region
    • Ensuring a message is sent once to all VMs
  • The demo app
    • How it works
    • How to get it up and running on Fly

Any suggestions/ideas are welcome!

2 Likes

I’m looking forward to this one because I have a project with web sockets I’m about to launch!

1 Like

Oh this sounds cool. Have you looked at open source options for events/pubsub? I’ve been meaning to try a Node app with nats.io running in the same VM. I really love apps that don’t require external services. In Go, I’d just embed NATS. I’m curious if there’s a way to get similarly simple with Node.

1 Like

I did look into NATS! But since the focus on the article is actually WebSockets, I went with Google as it seemed easier to set up.

Maybe we can do a NATS piece in the future :slight_smile:

Yeah if we get a node+nats starter example, it’ll make a lot more sense to use it in other demos.

1 Like

Here’s the first part of the article.

I will start working on the second part which focuses on the demo app and PubSub.

1 Like

The app is currently running here.

Here’s the Gist with the latest version of the article. I still need to work on a couple of things, but I’m getting there!

Here’s the complete first draft:

Any comments @kurt ?

Ooof I’m way behind, sorry for being so slow to respond to this. This looks really good. Do you want to post it on your blog (we’ll promote it) or have us run it on ours?

No worries @kurt !

As you prefer. I don’t care much about my blog to be honest :slight_smile: