Go database changes notifications over websocket. Generic solution. Reactive apps.

Do you know the problem when you’re writing single page apps and you have to manually update your data from the backend because you know you have just made a change and want the current state to update in “real time”? So you either load the data again by hand/initialite a refresh of the data, or you reload the page or you navigate to a redirect component to land back at your page so it’s loaded anew?

Well there are databases who can notify you of changes done. There’s RethinkDB, there’s Postgresql’s NOTIFY and LISTEN, there’s MongoDB’s change stream that never worked for me and I could never receive any help because IRC was always dead when I joined.

Anyhow, instead of relying on database to notify us that changes were made why not just have the changes in our handler or repository?

I wrote

https://git.icod.de/dalu/notifier

a library that deals with the problem.

So you need an instance of melody, created with melody.New()
You pass this instance to create a new Notifier instance and you pass this notifier instance to the handler if you use that.

GET / serves the index.html page that’s essentially a copy/paste of melody’s chat app and does connect and receive notifications via WebSocket. I mean that’s what it does, it returns the HTML that does it.

GET /ws is the WebSocket that is used to connect with

Now

with the paramter

That parameter is the callback function that is called when a notification is sent.
In this case the notification is marshalled into []byte and then broadcast by melody to all connected WebSockets.

Later in your handler, in this example we only have an INSERT handler without database code.

“Do database insert stuff” could be for instance

and then you continue with notifying the connected clients via WebSocket by writing

The very moment the notification is emitted it is sent(broadcast) to clients via WebSocket.
In your client-side webapp you receive that notification and filter by collection and do stuff like reloading entities or lists of entities.

By the way, if you don’t want to pass any data in the Notify function you can just write

Because you see the Notification type has the ,omitempty JSON tag.

The overhead of this whole ordeal is between 2-3 µs per notification.

You can also write

if you’d like it to be done at the end, when your response finished.

Note the go in front of the Notify function

Now in case you were wondering how to use a notification by WebSocket, here’s an example for Angular 6.

First the base web-socket-service-ts

Building upon the WebSocketService is the NoficiationService

And how you use this in your component, you do as you did before, you have an update data function where you grab the data from your endpoints, e.g.

Let’s keep it simple for now, not go crazy with switchMap etc.

In NgOnInit then you initially call this update function but also subscribe to the notification service.

The above means, if the updated collection is either containers or forums, grab fresh data from the endpoints.

Don’t forget to unsubscribe in NgOnDestroy() in any case.

So an example work-in-progress component.ts would look something like this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.