I'm using NativeScript and have implemented Pusher-Java Library as a dependency, I can successfully connect and subscribe to my Pusher channel, but I have truble adding a SubscriptionEventListener to my channel,
Here is my code which connects to pusher using java library in Nativescript:
module.exports = {
connect:function(app_key, channel_name, event_name) {
PusherOptions = com.pusher.client.PusherOptions;
Pusher = com.pusher.client.Pusher;
Channel = com.pusher.client.channel.Channel;
SubscriptionEventListener = com.pusher.client.channel.SubscriptionEventListener;
PusherEvent = com.pusher.client.channel.PusherEvent;
var options = new PusherOptions().setCluster("eu");
var pusher = new Pusher(app_key, options);
pusher.connect();
var channel = new Channel(pusher.subscribe(channel_name));
}
};
And here is the Java code for binding SubscriptionEventListener to the channel:
channel.bind("my-event", new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
System.out.println("Received event with data: " + event.toString());
}
});
now how can I bind this using Javascript!? I've tried anything I could come up with, but still couldn't bind the SubscriptionEventListener to channel with Javascript,
thank you
UPDATE
I'm using this method, which is expected to work and also @Manoj has answered down here:
channel.bind(event_name,
new SubscriptionEventListener({
onEvent: function(event) {
console.log(event.toString());
}
})
);
But it doesn't work and I get this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.plugintestproject/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err: > java.lang.AbstractMethodError: abstract method "void com.pusher.client.channel.Channel.bind(java.lang.String, com.pusher.client.channel.SubscriptionEventListener)"
System.err: com.tns.Runtime.callJSMethodNative(Native Method)
A couple things:
Why not just use the nativescript-pusher plugin? It already exists...
Second if you don't want to use it; why not borrow the code as it is under a Apache 2.0 License.
However, to specifically answer your question:
const sel = new com.pusher.client.channel.SubscriptionEventListener( {
onEvent: function(channel, event, data) {
console.log("Channel:", channel, "Event", event, "received event with data: " + data.toString());
}
} );
First, you really should use the FULL namespace when creating an event (this makes it obvious what is being created ).
Second, your prototype for onEvent
was wrong. According to the docs, it is Channel, Event, Data
are the parameters passed into it.