Gihan Lasita
Gihan Lasita

Reputation: 3055

how to communicate with a desktop application from browser?

Is it possible to communicate with a desktop application from browser?

I want to do something like this,

Let's say there is a button in my web application with a URL to a data source and when button is clicked desktop application opens and get that data source URL and process data with desktop application.

Is it difficult to do such thing? Any examples?

Upvotes: 23

Views: 25524

Answers (7)

NP Rooski  Z
NP Rooski Z

Reputation: 3657

In addtion to Alex K's answer for windows... for those looking for solution on macOS and Linux.

Linux

Most of the modern distros implement freedesktop standards and one of them is desktop files. You can create a desktop file with [service] section.

$ cat test.desktop 
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Comment=My test app
Name=TestApp
Icon=TestIcon
Exec=/opt/test/test.sh %u
DBusActivatable=true
Categories=Network;
MimeType=x-scheme-handler/test;  <------ This is handler for test://somedata URLs 
NoDisplay=false

Copy this file in /usr/share/applications/test.desktop

macOS

Simply add something like following in your applications Info.plist file

    <array>
            <dict>
                    <key>CFBundleTypeIconFile</key>
                    <string>/tmp/test.png</string>
                    <key>CFBundleTypeRole</key>
                    <string>Viewer</string>
                    <key>CFBundleURLName</key>
                    <string>com.mytest</string>
                    <key>CFBundleURLSchemes</key>
                    <array>
                            <string>test</string>  <---- This is handler for test://somedata URLs hit on browser
                    </array>
            </dict>
    </array>

Upvotes: 1

user5292841
user5292841

Reputation:

You can easily add Fleck WebSocket server to your desktop application, and then access this using Websocket.

Note: Only Windows 8 and 10 support WebSockets through Microsoft's WebSockets implementation, but Fleck will work with Windows 7.

https://github.com/statianzo/Fleck It's quite easy to add Fleck to your project using NuGet Package Manager:

Install-Package Fleck

Here is the echo example from Fleck webpage (add this to the C# program to execute during startup):

var server = new WebSocketServer("ws://127.0.0.1:8181");
server.Start(socket =>
{
  socket.OnOpen = () => Console.WriteLine("Open!");
  socket.OnClose = () => Console.WriteLine("Close!");
  socket.OnMessage = message => socket.Send(message);
});

In the javascript:

var exampleSocket = new WebSocket("ws://127.0.0.1:8181", "protocolOne");

exampleSocket.send("Here's some text that the server is urgently awaiting!");

//and receive (make a listener for the socket) :
exampleSocket.onmessage = function (event) {
  console.log(event.data);
}

Upvotes: 5

inor
inor

Reputation: 2846

the desktop application should embed a small server in it, like Jetty. Since the browser content source domain (e.g. www.myDomain.com) is different than the localhost domain of the Jetty, you would run into security problems. These should be overcome by the use of CORS (Cross Origin Resource Sharing) which is a new standard. Using CORS, the Jetty server tells the browser that it/localhost allows Cross domain access to its resources if the requests originate from the source domain www.myDomain.com. For security reasons, i would also make the Jetty reject any request whose source ip is not localhost

Upvotes: 2

robisrob
robisrob

Reputation: 960

Here is a clunky suggestion, but I think worth mentioning all the options since the custom URI and running server solutions are pretty involved... Generate a small file containing the parameters of interest, with a custom extension associated with your desktop app. So when the user clicks the browser button they will have to go through the browser's file download dialog/toolbar and maybe some annoying security verification popups... not ideal user experience, but might be the easiest way to implement this type of communication, and doesn't require a process running in the background like a server.

I have a web app used within my company for interfacing to old databases and poorly organized files. I need a way to allow the users to open the actual files from the network and not download copies, so they can be edited in place. Considering a solution like this or the custom URI scheme so that a small executable not running in the background can simply be passed the filename and open it for the user directly.

Upvotes: 1

Alex K.
Alex K.

Reputation: 175748

On windows its trivial to create a custom URL Protocol that's invokable via

<a href="whatever://somedata">..</a>

This works in IE, FF and Chrome, although in the latter the link must be opened via javascript to avoid omni-bar confusion.

Upvotes: 16

codejitsu
codejitsu

Reputation: 3182

Hm, you need something like client-server application. The server is a lightweight http server, which is waiting for messages from the client (browser). The browser can communicate with your server via ajax for example.

Upvotes: 1

Alex Turpin
Alex Turpin

Reputation: 47776

You will need to have something running on the deskop, like a server, and make a request to it for the server to open up an application. You could do it with a Node.js. Of course that requires the server to be running on the client's desktop.

The alternative would be to make a browser extension / plugin, and have people install that. Those extensions could probably launch an application on the desktop.

Upvotes: 10

Related Questions