3 ways of Web — desktop app communication
I recently had to find a way to make a web application communicate with a desktop one. I wanted to launch the desktop app from the web, and exchange messages between the two.
It was not easy to find what are the possibilities, so I decided to write a recap based on the investigation I made. I found 3 possible ways of doing it, depending of what you want to achieve.
Protocol handler for one-way communication
If you just want to launch your desktop app from the web, eventually passing some values, you can use custom “protocol handlers”.
So how does it work? Well when you’ll install your desktop app, the setup program will add some new registry keys. These keys are just here to say “hey now when any web browser goes to a URL starting with myprotocol:// then you will execute my program”. Then, next time you try to access a URL like myprotocol://anything/you/want, your desktop app will be called with the URL given as first parameter (argv for C/C++). So any value you might want to pass would go in the URL.
Pretty simple right? All you have to do is on your setup program, implement the registry keys config, and of course in your main program handle the URL to get and process the input values. This MSDN article explains in detail how does it work.
OK but now what if I want my desktop app to send back a message like “hello I’m open” to the web app?
Chrome extension and Native Messaging for simple two-way communication
Chrome extensions have really nice features : they can exchange messages with Web applications, and they can also “talk” to desktop apps. The latter is called “Native Messaging”.
Basically this is a way to launch a desktop app and communicate through standard input /standard output. You already understood the extension would be the bridge between web and desktop! To implement it you would have to:
- Create the extension
- Make it communicate with the web app
- Make it communicate with the desktop app (and implement in the desktop app the communication through standard input / output)
There is some serious work to do here! But you’ll see that actually it’s pretty easy to implement. Check the Native Messaging documentation.
When you have it all set it works well, but there is something to know : for now this only works in Chrome and Opera. Firefox plans to support native messaging, and for Edge we don’t know as of today if they will support it or offer another way of doing it. So be careful as it is not standard yet (we hope it will be!).
Server and WebSockets for “rich” two-way communication
If you need your web app to be very “chatty” with your desktop application, then it would be a good idea to implement a server on the desktop side.
Now with all modern browsers you can use WebSockets : it’s a quite simple way to communicate with a server through a specific implementation of sockets.
You can either implement yourself the WebSockets protocol (huge piece of code on the server side), or you can use a library to do the “dirty work” for you and focus on the essential.
I recommend that you use a library, and in particular socket.io does the job and is widely used so you can find a lot of resources to help you.
I found these 3 solutions to be viable in the modern web, each one having its benefits: you have to choose the one that best suits your needs.
I hope this article will be helpful if like me you were a bit lost about which way to go to close the gap between your web and desktop app!
❤ Hit the heart icon and spread love ❤
Like and share this post if you found it helpful!
NB: I purposefully did not talk about Java or even ActiveX as they are disappearing from modern browsers. I did not talk either about Firefox ctypes as it’s Firefox only (and probably not gonna be standardized as a difference with the Chrome extension solution).