Angular multipart/formdata

You would like to upload or post something not as an application/json but as a multipart/formdata.

It’s pretty simple, simpler than you would’ve thought 🙂

1. create the FormData instance

then you can use .set or .append methods to add values to the post body of the FormData.

and finally we upload it or send it away, rather.

if we have a file upload and would like to be notified about upload progress

when we subscribe to this, we have several events happening.

Angular, EventSource, Go and wasted lifetime

If you have ever used EventSource and Angular and have read all the other blog posts and github issues and Stackoverflow posts and nothing worked, then you’ve probably come here and will happily leave knowing you solved your problem.

First of all I use cobra, I know, get to the point. As part of cobra I add the serve command, so I can go run main.go serve

This is the serve.go file

I set up the route, cors and run it

The server/handler/handler.go file

Here is the important part. I wasted the last 6 hours and previous to that 2 days on this issue.
If you’re serving this via nginx, you have to set this header X-Accel-Buffering = no.
If you don’t send this header responses will get buffered by nginx until the timeout it met then flushed to the client.
The above code has a ticker that ticks every second and sends a new “Server Sent Event”.

Why it didn’t work for me was, as you see above Event: "message". I had that set to “darko”.

The Angular service

eventSource.onmessage expects a message with the Event: "message" content. Since I had it set to “darko”,
the onmessage event never fired. If you for whatever reason need to send an event that is not a message type,
the eventSource.addEventListener is how you listen for that event.
As you might have seen in other blog posts or github issues, zonejs and EventSource aren’t the best of friends.
So you have to wrap it all in so you can have real time updates, and not just when you unsubscribe from the Observable.

Finally, the component

and the component html

Finally, the nginx configuration for the development server. To serve it all.
Here I’m using on the local network.

libvirt manual migration

Following scenario:

You have 2 servers. No shared storage.
You would like to move VMs from Server A to Server B.

If you want a clean filesystem there will be downtime.
Downtime will take as long as it needs to copy the image from one server to the other.

Step 1:

Shutdown the instance running on the source server.

Step 2:

Copy the image, usually located in /var/lib/libvirt/images/ , from source to destination.

Step 3:

Dumo the configuration xml of your vm and copy it to the destination server

This will copy to the home directory of the destination server, in this case /root/ (implied by the : after the server name)

Step 4:

Import the copied xml domain configuration on the destination server.
The MAC address probably changed, depending on your data center’s setup you’d need to edit the xml file accordingly.
The domain import command is

Step 5:
Finish it up.
Change network configuration from inside the guest.
You will need to login via spice console.
It’s best to use the great tool “Virtual Machine Manager” for this job.
Remember to keep your VM’s root password ready.

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

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


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