olofd / react-native-signalr

Use SignalR with React Native
150 stars 61 forks source link

Response from proxy.invoke promise not getting returned #1

Closed chrisgregg closed 8 years ago

chrisgregg commented 8 years ago

Thanks for the work on this. One issue I'm experiencing is when making a call to the server with proxy.invoke, the response that I'm sending from the server back to client doesn't seem to be getting returned via the promise.

For example:

        let messagePromise = proxy.invoke('messageToServer', 'hej');
        messagePromise.done((response) => {
            console.log ("response", response');
        }).fail(function (error) {
            console.log('Invocation of NewContosoChatMessage failed. Error: ' + error);
        });

When I log out the response from the promise, I'm seeing a different object that looks something like this:

{state: Object, connection: h…n.fn.init, hubName: "hubname", _: Object, events: Object}

olofd commented 8 years ago

Hmm, I'll look into this tonight!

olofd commented 8 years ago

@chris-gregg Sry for taking such a long time before looking at this. But it think that what you are receiving in the done-callback is correct. If you want to receive the actual message sent from the server you should set up a listener before calling invoke like this:

        proxy.on('responseFromServer', (response) => {
           console.log("FROM SERVER", response);
        });
        let messagePromise = proxy.invoke('messageToServer', 'hej');
        messagePromise.done((response) => {
            console.log ("response", response');
        }).fail(function (error) {
            console.log('Invocation of NewContosoChatMessage failed. Error: ' + error);
        });

I have not looked at if the signalr-client used in a browser does the same, but I expect it does. The done for the message is only a status about that particular message.

I will closed this for now.

chrisgregg commented 8 years ago

Hey @olofd, thanks for taking a look. With SignalR you can receive data from the server via a listener/callback as you suggested, but should also be able to access a response from the server via the promise.

I was going to take a look and submit a pull request, but noticed in your last commit that you actually fixed the problem! Looks like the issue was that resolveWith was originally returning the proxy, and not the data (response).

So I can now do this, and successfully retrieve the response:

let messagePromise = proxy.invoke('messageToServer', 'hey');
messagePromise.then((response) => {
     console.log ('response', response);
}).fail(function (error) {
     console.log('Invocation of NewContosoChatMessage failed. Error: ' + error);
});

Thanks again!

olofd commented 8 years ago

ah! Great! Sry for the incorrect answer before! Let me know if there are any other issues with the lib!

chrisgregg commented 8 years ago

Will do, looking forward to using this!

olofd commented 8 years ago

@chris-gregg Can you take a look at this: https://github.com/olofd/react-native-signalr/pull/2

I think there should be an apply there, elsewise I get my data wrapped in an array. Have you experienced this?

chrisgregg commented 8 years ago

Yeah, you're right. I'm seeing the response encapsulated in an array too.

olofd commented 8 years ago

Great! Thanks for the confirmation. I'll merge and release a new version then.

olofd commented 8 years ago

Release 0.0.6