0

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)


KEYHAN
  • 104
  • 6

3 Answers3

1

A couple things:

  1. Why not just use the nativescript-pusher plugin? It already exists...

  2. 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.

Nathanael
  • 5,369
  • 18
  • 23
  • Thank you Nathanael for detailed information, I wanted to make it native as possible, now how can I bind it to the channel? I don't get anything at console.log(); and tried to channel.bind() but got error – KEYHAN Oct 02 '19 at 07:19
  • I get Abstract method error when I try to bind eventListener to channel: System.err: > java.lang.AbstractMethodError: abstract method "void com.pusher.client.channel.Channel.bind(java.lang.String, com.pusher.client.channel.SubscriptionEventListener)" – KEYHAN Oct 02 '19 at 07:36
  • 1
    If you are getting abstract; are you sure you have a valid channel; that sounds like you have created a new empty channel directly from the channel object and didn't create the channel via the pusher interface correctly which gives you the properly built channel object. What happens if you do: `const channel = connectedPusher.getChannel(channelName);` do you get a valid channel; or a null/undefined. – Nathanael Oct 02 '19 at 16:05
0

SubscriptionEventListener is an interface, you should implement the methods and pass the instance to bind method as showcased in docs.

channel.bind("my-event", 
   new SubscriptionEventListener({
    onEvent: function(event) {
        console.log("Received event with data: " + event.toString());
    }
   })
);
Manoj
  • 21,753
  • 3
  • 20
  • 41
  • I've tried the same, and tried again now, but I get this error: ``` Caused by: 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) ``` – KEYHAN Oct 01 '19 at 14:34
  • I'm not familiar with the library. But instead `new Channel(pusher.subscribe(channel_name));`, the examples in the repo uses `pusher.subscribe("my-channel", new ChannelEventListener({...})`. Did you try that? – Manoj Oct 01 '19 at 14:46
  • I tried now, got this: java.lang.Exception: Failed resolving method subscribe on class com.pusher.client.Pusher – KEYHAN Oct 01 '19 at 14:53
  • I don't see why, because you were already using subscribe method in your code. Did you try generating typings? – Manoj Oct 01 '19 at 15:05
0

thanks to Nathanael Here is the final code:

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;
        PusherEvent = com.pusher.client.channel.PusherEvent;
        SubscriptionEventListener = com.pusher.client.channel.SubscriptionEventListener;
        ChannelEventListener = com.pusher.client.channel.ChannelEventListener;

        const options = new PusherOptions().setCluster("eu");
        const pusher = new Pusher(app_key, options);

        pusher.connect();

        const channel = new Channel(pusher.subscribe(channel_name));
        const connectedChannel = pusher.getChannel(channel_name);

        let sel = new SubscriptionEventListener({
            onEvent: function(event) {
                console.log(event);
            }
        });

        connectedChannel.bind(event_name, sel);
    }
};

KEYHAN
  • 104
  • 6