In Salesforce, postMessage is mainly used for communicating in between Visualforce and Lightning components


The window.postMessage() method safely enables cross-origin communication between Window objects

The description and intent of this is quite clear, however, if we use it to send/receive data across components belonging to different namespaces (with api v40+ = Locker Service), for example:

c:cmp1 and namespace:cmp2

because, the namespaced component does not provide any event to do so natively within lightning (using app or cmp events) but does provide the capability of attaching a script (where this can be implemented easily), would using postMessage be a convenient and secure approach?

If not - is there a better way to communicate in between components that bellong to different namespaces without native lightning events?

  • No, postMessage wouldn't technically be appropriate here. Ideally, the managed package owner should fix the component. – sfdcfox Jan 4 at 17:08
  • Pardon my dumb question. I'm confused about what exactly you're asking ?, But Let's say if namespace:cmp2 raised an Application event your c:cmp1 should be able to handle it as long as you know the what are the params you need look for. There is no need for postMessage here. – codeyinthecloud Jan 4 at 17:28
  • thats the thing - there is no applicatiton event or whatsoever - so - essentially, the only thing I can think of was using a postMessage, at least while the managed package does any changes to their components. In essence, asking what is the downside of using postMessage – glls Jan 4 at 17:54
  • Ah got you now. Like @sfdcfox mentioned package owner has to fix it rather than using postMessage! – codeyinthecloud Jan 4 at 17:57
  • If the namespaced component say updates a record or you can update a record by the capability of the script that you can attach, one approach I can think of is to write a trigger on that particular object to raise a platform event, and then subscribe it in your custom component? – Jayant Das Jan 4 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.