Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Cross-Application Messaging with LocalConnection in Adobe AIR

Tech 1

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.