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 (@Component annotation) and implement the call method, which will be executed on an event call.

Example:

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

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

@Component
public class MyEventName implements IActionHandler<String> {
	@Override
	public Object call(String payload) {
		System.out.println(payload);
		return null;
	}
	...

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

In adition, you can listen for more events in the same class by adding more methods annotated with @EventName.

Example:

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

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

@Component
@EventName("someOtherEventName")
public class MyEventName implements IActionHandler<String> {
	@Override
	public Object call(String payload) {
		System.out.println(payload);
		return null;
	}

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

	@EventName("secondcustomevent")
	public void mySecondEvent(String payload) {
		System.out.println("Second event: " + payload);
	}

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

	...

Calling an Event from Javascript

Once registered, we can call it in a Js script using the Equo Communication API like this:

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

Listening an Event in Javascript, call it from Java

The other way works the same.

Listening 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");
	}

	...