Java and Javascript Communication

One of the main features of the Equo Framework is its capability to create an application combining web interfaces with the Java programming environment. To do this, we need a bidirectional communication between Java and Javascript code.

The solution is the Equo Communication API. The API is based on events: you register an event listener somewhere (Js or Java code) and then from somewhere else you can call it. It’s that easy.

Below we will show some simple use cases, but you can refer to Communication js API and Communication Java API to read the full APIs documentation.

Listening an Event in Java, call it from Javascript

Let’s show how to execute Java code from Javascript.

Listening an Event in Java

To listen for events you have to create a class that implements the IActionHandler interface.

This class needs to be a component (can be done by simply adding the @Component annotation). Any method defined in this class will be added as an event listener, the methods may declare 0 or 1 parameters.

Example:

import org.osgi.service.component.annotations.Component;

import com.equo.comm.api.actions.IActionHandler;

@Component
public class MyEventManager implements IActionHandler {

	public void printPayload(String payload) {
		System.out.println(payload);
	}

	...

By default, the event name will be the method name. This can be changed by adding the @EventName annotation to the method with the desired event name as the value.

As described above, you can have any number of action handlers defined in this class.

Example:

import org.osgi.service.component.annotations.Component;

import com.equo.comm.api.annotations.EventName;
import com.equo.comm.api.actions.IActionHandler;

@Component
public class MyEventName implements IActionHandler {

	@EventName("firstCustomEvent")
	public String myFirstEvent(String payload) {
		System.out.println("First event: " + payload);
		return payload;
	}

	@EventName("secondCustomEvent")
	public SomeObject mySecondEvent(String payload) {
		System.out.println("Second event: " + payload);
		return new SomeObject(payload);
	}

	public void myThirdEvent(String payload) {
		System.out.println("Third event: " + payload);
	}

	...

Calling an Event from Javascript

Once you have some action handlers registered, you can call them from a JS script using the Equo Communication API like this:

equo.send("myThirdEvent", "String printed from Javascript");

When an event expects a response from Java, you can also use promises. The appropriate action handler will be called and its return value will be used to resolve the promise.

---
equo.call({actionId: "mySecondEvent", payload: { param1: "someValue" } })
	.then((response) => {
		console.log(response)
	});
---

Listening to an Event in Javascript, call it from Java

The other way works the same.

Listening to an Event in Javascript

To listen for an event, you just need to use the Equo Communication API like this:

equo.on("myeventname", function(payload) {
	console.log("Received payload: " + payload);
});

Calling an Event from Java

Get a reference of the IEquoEventHandler component implementation. Then, send an event.

Example:

import org.osgi.service.component.annotations.Reference;

import com.equo.comm.api.IEquoEventHandler;

public class SomeCustomClass {
	@Reference
	private IEquoEventHandler equoEventHandler;

	...

	void someFunction(){
		equoEventHandler.send("myeventname", "Message from Java code");
	}

	...