Cross-Application Messaging with LocalConnection in Adobe AIR
LocalConnection enables message passing between separate Adobe AIR application instances running on the same system. This mechanism allows one application to broadcast data to multiple listeners using named connection channels.
To establish communication, applications designate unique identifiers following the app#AppID:ChannelName format. The sender specifies the target application's ID and the shared channel name when transmittnig data.
Broadcasting Applictaion
The transmitting side initializes a LocalConnection instance and dispatches messages to specific receivers or multiple targets sequentially:
import flash.net.LocalConnection;
import flash.events.StatusEvent;
import flash.events.MouseEvent;
var channel: LocalConnection;
function initializeTransmitter():void {
channel = new LocalConnection();
channel.addEventListener(StatusEvent.STATUS, handleTransmissionStatus);
dispatchBtn.addEventListener(MouseEvent.CLICK, transmitToAppB);
multicastBtn.addEventListener(MouseEvent.CLICK, transmitToMultiple);
}
function transmitToAppB(evt:MouseEvent):void {
channel.send("app#ReceiverB:mainChannel", "processData", inputField.text);
}
function transmitToMultiple(evt:MouseEvent):void {
channel.send("app#ReceiverB:mainChannel", "processData", inputField.text);
channel.send("app#ReceiverC:mainChannel", "processData", inputField.text);
}
function handleTransmissionStatus(evt:StatusEvent):void {
if (evt.level == "error") {
trace("Message delivery failed");
} else if (evt.level == "status") {
trace("Message delivered successfully");
}
}
Receiving Application
Each listener binds to a specific channel name and exposes methods that the sender can invoke remotely:
import flash.net.LocalConnection;
import flash.events.ArgumentError;
var inboundConnection:LocalConnection;
function initializeListener():void {
inboundConnection = new LocalConnection();
inboundConnection.client = this;
inboundConnection.allowDomain("app#SenderA");
try {
inboundConnection.connect("mainChannel");
} catch (err:ArgumentError) {
trace("Channel unavailable - already claimed by another instance");
}
}
public function processData(payload:String):void {
display.appendText(payload + "\n");
}
Multi-Receiver Configuration
Additional receivers follow an identical pattern, connecting to the same channel name while exposing the required callback methods:
import flash.net.LocalConnection;
var receiverLink:LocalConnection;
function setupReceiverC():void {
receiverLink = new LocalConnection();
receiverLink.client = this;
receiverLink.allowDomain("app#SenderA");
try {
receiverLink.connect("mainChannel");
} catch (e:ArgumentError) {
trace("Connection rejected - channel occupied");
}
}
public function processData(content:String):void {
logField.appendText(content + "\n");
}
The allowDomain() method explicitly permits cross-application communication from specified sources. Without this security declaration, the runtime blocks incoming connections between distinct AIR applications. Each receiver must connect to the identical channel string declared in the sender's send() invocation.