Sending and receiving messages

March 17, 2010 by: admin

The base functionality delivered by easyXDM is to, without any server-side dependencies,  pass string based messages between two domains in a fast, reliable and secure way.

With easyXDM this is easily done using the easyXDM.Socket class, a class that encapsulates the different transports and behaviors necessary to achieve speed, reliability and security.

To receive messages you simply set the onMessage method in the configuration

var socket = new easyXDM.Socket({
    ...,
    onMessage: function(message, origin){
        alert(message);
    }
});

.. and to send messages you simply use the instantiated sockets .postMessage method

socket.postMessage("the string I want to send to the other end");

Remember, each Socket is bi-directional, so you only need to instantiate a single one per channel.
Take a look at the example here.

  • 123

  • Anonymous

    I don’t quite understand why setting
    swf: “../easyxdm.swf”.
    Anyone can tell me why?

    • Spoike

      For the IE6/7 browsers messaging is implemented using FlashTransport. This is why you need to specify where the easyxdm.swf file is.

      • Desmond Nzuza

        do you know if it slows things down if you have it specified even on browsers that are not I.E?

  • Frustrated Reader

    Anyone else think the referenced example is terrible? It just links you to a webpage that happens to use easyXDM. There are no pointers to the code that was implemented. The reader is left to slog through the page source to figure it out. How about just listing the relevant code on the page???

    • User

      Totally agree.

    • creppie

      Could use work but these examples are functional.  That’s good enough for me.  Up and running in an hour or so for my proj!

  • Tufik Chediak

    What is … in your example, please:

    var socket = new easyXDM.Socket({
    …,
    onMessage: function(message, origin){
    alert(message);
    }
    });

  • Stephen

    This is one of the most confusing things I’ve ever read, everything from downloading this code to implementing it. Coming from a javascript expert. We need to include the easyXDM.js file on both the parent and the host?
    Yet the easyXDM does a ‘document.write’ of like 10 scripts. Having that on all the cross domain hosts is unpleasant at best.

    You’re telling me Twitter sends messages cross domain using their twitter button by including these 10 scripts? I find that hard to believe.

  • David Leonard

    The example for provider should read:

    var socket = new easyXDM.Socket({

    onMessage:function(message, origin) {

    // not here

    }

    });

    // here
    socket.postMessage(‘hello world’);

  • Emanuele Ciriachi

    Can anyone point to a page with a more coincise example?