Skip to content
This repository has been archived by the owner on Sep 7, 2018. It is now read-only.

Messaging API

Ben Lambert edited this page Mar 24, 2017 · 4 revisions

The messaging API is used to send messages between windows.

Initial Implementation

Current Support

  • Browser - window.postMessage() can be used to send messages to other windows, as long as there is a reference to the destination window.
  • Electron - No built in way of doing pure window to window communication. Can be done via ipc calls if the other window is known (by id, for example)
  • OpenFin - Using fin.desktop.InterApplicationBus, messages can be send to one or multiple windows. The send method takes 4 parameters, (application uuid, window name (optional), topic, message). By providing both the application uuid and the window name, the message bus will send the message to that specific window. The window name can also not be passed, which causes all windows with the application uuid to receive the message. Windows must subscribe to the correct applciation uuids, windows and topics to receive messages, using subscribe(uuid, name, topic, listener) where listener is called when a message is received.

Current Implementation

Initialy, OpenFin's implementation was followed. However, electron has no concept of applications, so instead only 1 id is passed to the send/subscribe functions. In the case of Electron, this is the window Id. For OpenFin, this is the application uuid and the window name concatenated together with a :, e.g. uuid:name.

  • Browser - Partial implementation using window.postMessage(), see below for details.
  • Electron - Uses ipc calls, passing the destination window id, topic and message to the main process. The main process then uses the window id to get the browser window object, which can then be sent data via window.send(...). Subscribing is done by passing the window id of the window that is sending the message (similar to OpenFin implementation).
  • OpenFin - Reuses the native OpenFin implementation, but splits the window id passed in back to the application uuid and window name.

Browser implementation

A number of ways of implementing browser window to window messaging were discussed. Initially, messaging was done via window.postMessage, which worked well between the creating window and the created window, but was unable to send messages to windows that were not created from or by the current window. For example, window W1 creates 2 windows, W2 and W3. Using this method, W2 and W3 cannot send messages between each other, without going through W1.

The second method was to use localStorage and add a listener for when the storage changes. This seemed promising as the logic for window name/topic filtering could be done in the listener. However, in Chrome and FireFox, the storage event did not fire in windows created via window.open when any features are passed in as an argument. Because of this limitation, the first implementation was chosen.

A third option that was looked at was WebRTC, but sending messages using this required using websockets and a server, see here, which was out of scope of this API.

Clone this wiki locally