/
Server to Client Communication

Server to Client Communication

It is possible to send JavaScript code to be executed on the client browser from a C++ module loaded on server.

Run JS code on client browser in Kudo Editor iframe

The JS code you want to execute on client can be sent from server to client as a string using WApplication’s doJavaScript function (SDK/Headers/wt_inc/Wt/WApplication). Your module needs to link against Wt lib located in SDK (SDK/Libraries/wt_lib/).

  1. Include XeUpdateLock.h from SDK (SDK/Headers/inc/) - it will also include Wt/WApplication

  2. Take XeUpdateLock before getting WApplication object

  3. Get WApplication object and call doJavaScript on WApplication object

Example how to print something to console:

XeUpdateLock lock; if (lock) { Wt::WApplication::instance()->doJavaScript("console.log('This message is printed to console');"); }

Note: If you do not take XeUpdateLock, Wt::WApplication::instance() will return a null pointer.

The JS code is evaluated on client by calling window.eval() function before run and an exception will be thrown if it is not valid what will cause a client disconnect.

Post a message from Kudo Editor iframe to Partner Web Application

It is possible to post data from Kudo Editor iframe to the Partner Web Application directly on the client browser. A Partner Web Applications is referred as “parent” in our SDK.

Using our SDK

Our SDK provides two convenient C++ functions for the communication from Kudo Editor loaded in an iframe to a Partner Web Application, see API Reference.

void XeIntegration::postStringToParent( std::string message ); void XeIntegration::postJSONMessageToParent( std::string messageName, std::string messageContent );

We also provide a ready-to-go example application “CrossOriginIntegration”, see samples. This example is a custom module that can be loaded by Kudo Server. 

Using postMessage

In addition to the helper functions, the Web API function “Window.postMessage()” can be used directly in your module. For that, JavaScript code executing this function must be transferred from Kudo Server to the client browser. 

Sending a simple string from the Kudo Server to its iframe and from there to the parent (your web application) looks like this:

XeUpdateLock lock; if (lock) {     std::string js = "if (window.parent) {"                          "window.parent.postMessage("my message", '*');"                      "}";     Wt::WApplication::instance()->doJavaScript(js); }

Related content

Partner Web Application → Kudo Editor iframe
Partner Web Application → Kudo Editor iframe
More like this
API Server: Connecting ARES Kudo to a data backend
API Server: Connecting ARES Kudo to a data backend
Read with this
Kudo Editor iframe → Partner Web Application
Kudo Editor iframe → Partner Web Application
More like this
Embedded Widget communication
Embedded Widget communication
More like this