-
Notifications
You must be signed in to change notification settings - Fork 10
Messaging API
The messaging API is used to send messages between windows.
-
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. Thesend
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, usingsubscribe(uuid, name, topic, listener)
where listener is called when a message is received.
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 viawindow.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.
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.