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

Hetzner, das Forum und Sabotage oder doch alles nur bedauerliche Zufälle?

Hetzner hat ein Forum, und ich sag immer, je billiger die Preise, desto dümmer und frecher die Menschen.
So wird man dann und wann gern von irgendwelchen Waldemars angegangen weil man nicht das schreibt das ihnen passt oder sie einfach mal wieder sexuell frustriert zu viel am CPU-Lüfter geschnüffelt haben.
Und so wie ich bin, ich lass mir nichts gefallen und schieße natürlich entsprechend zurück.

Das führt dann zu seltsamen Zufällen:

Ein Server mit 4 Festplatten im Hardware RAID10 verliert plötzlich 2 Festplatten, zeitgleich. Es muss komplett das System neu installiert werden.
Was ich denke das passiert war? Der Betroffene im Forum war ein Hetzner Mitarbeiter der meinte seine Wut dann an der gemieteten Hardware auslassen zu müssen.
Beweisen kann ich es nicht. Und er hat entweder einfach 2 Kabel abgezückt oder sonst irgendwie eine Möglichkeit gefunden das zu veranstalten. Ist doch komisch das es genau 1-2 Tage nach dem Zoff passierte.

Der selbe Zoff, anderer Server. Morgens 10 Uhr drückt einfach jemand den POWER-Knopf auf dem Server was dazu führt das der Server heruntergefahren wird und 2 Stunden offline war, bis ich es merkte. Hier ein Screenshot als Beweis:

Gestern oder vorgestern wieder im Hetzner Forum pflaumt mich so ein Dorfdepp wieder dumm von der Seite an und ich erwider es natürlich. Und ich dachte mir schon das schon wieder sowas passieren würde. Und was ist? Natürlich 1 der 4 Festplatten war plötzlich nicht mehr vorhanden im System. Welch Zufall.

Beweisen kann man natürlich nichts. Aber sollten sich nochmal solche Zufälle ereignen kann ich trotzdem mal Strafanzeige gegen Unbekannt machen.