Skip to content

Instantly share code, notes, and snippets.

@matheusfrancisco
Last active January 18, 2021 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matheusfrancisco/aa226458ac656bda9ab1dc6b9646389a to your computer and use it in GitHub Desktop.
Save matheusfrancisco/aa226458ac656bda9ab1dc6b9646389a to your computer and use it in GitHub Desktop.
(ns example.core
(:require [messenger.core :as msg]
[cljs.core.async :refer [<!]])
(:require-macros [messenger.macros :refer [deflistener]]
[cljs.core.async.macros :refer [go]]))
; Define an iframe to communicate with.
(def iframe (js/document.getElementById "iframe"))
; Define a listener function that handles requests.
(deflistener frame-listener [req]
; Declares :hello action - get :name from the request map and display the hello alert.
; Return empty response map.
(:hello
(js/alert (str "hello, " (:name req)))
{})
; Declares :get-time action - return current time to the calling window.
(:get-time
{:time (.getTime (Date.))}))
(go
; Initialize messaging target and origin.
(set! msg/*target* (or iframe.contentWindow iframe))
(set! msg/*origin* "http://example.com"))
; Set up the listener.
(msg/listen frame-listener)
; Send :get-some-info request to the target (the iframe) and then display the response.
(js/alert (<! (msg/request :get-some-info {:another-info "test"}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment