Azure / azure-relay-node

☁️Node.js library for Azure Relay Hybrid Connections
https://docs.microsoft.com/en-us/azure/service-bus-relay/relay-what-is-it
MIT License
12 stars 15 forks source link

Looking for sample for bidirectional communication #11

Closed ddobric closed 7 years ago

ddobric commented 7 years ago

Hi all,

looking for a sample, which demonstrates how listener can response to received message. For example, following code fails:

ws.onmessage = function (event) {
    console.log("RCV " + event.data);
     ws.send("HELLO", function(err){
        console.log(" send failed :(" );
     });
 };

Not sure if this the right way to do it. Unfortunately, the code above fails with 'unknown' error.

Thanks Damir

jtaubensee commented 7 years ago

Hi @ddobric - I think you are on the right track. Which library are you using? I was able to test the following successfully with hyco-ws.

var wss = WebSocket.createRelayedServer(
    {
        server : WebSocket.createRelayListenUri(ns, path),
        token: WebSocket.createRelayToken('http://' + ns, keyrule, key)
    },
    function(ws) {
        console.log('connection accepted');
        ws.onmessage = function(event) {
            console.log('onmessage: ' + event.data);
            ws.send('message received: ' + event.data);
        };
        ws.on('close', function() {
            console.log('connection closed');
        });
    }
);

The other thing to keep in mind here is that you will then have to add the following to your sender application if you want to see anything on the other side.

ws.onmessage = function(event) {
    console.log('onmessage: ' + event.data);
};
ddobric commented 7 years ago

Hi @jtaubensee ,

I used exactly the same lib.

const WebSocket = require('hyco-ws');

var wss = WebSocket.createRelayedServer(
     {
         server : WebSocket.createRelayListenUri(ns, path),
         token: WebSocket.createRelayToken('http://' + ns, keyrule, key)
     }, 
     function (ws) {
         console.log('connection accepted');

      ws.onmessage = function (event) {

            console.log("RCV " + event.data);

             ws.send("HELLO", function(err){
                console.log(" send failed :(" );  ****
             });
         };

         ws.on('close', function () {
             console.log('connection closed');
         });      

           ws.on('open', function () {
             console.log('connection open');
         });    
 });

 console.log('listening');

 wss.on('error', function(err) {
     console.log('error' + err);
 });
}

This code fails in line marked with '***'. 'err' argument is undefined. This shouldn't be the case. Btw. the sender side is .NET (hyco-relay) code. Due the broker in the middle, this shouldn't have an impact, but it might be useful information.

ddobric commented 7 years ago

?

jtaubensee commented 7 years ago

@ddobric - can we continue this in #12? I mean is there anything else that you are looking for regarding a sample?

ddobric commented 7 years ago

Hi @jtaubensee , Yes, the sample provided above does not send messages back to relay.

my issue here is that following code simply fails:

 ws.send("HELLO", function(err){
        console.log(" send failed :(" );
     });

In #12 we can follow missing error object.

jtaubensee commented 7 years ago

OK, so I will close this issue and we can discuss the missing error object in #12. Thanks.

ddobric commented 7 years ago

Hi John, didn't get this. I have two issues: 1) #12 is right place for missing object. 2) However, issue here is that send does not work.

dlstucki commented 7 years ago

Again I'll ask how do you know the send is broken? Perhaps there's a bug on the other side in processing what was sent?

ddobric commented 7 years ago

I know because, it enters in on-error function and message is not received on the receiver (in this case, listener on the client side).

 ws.send("HELLO", function(err){
        console.log(" send failed :(" );
     });
dlstucki commented 7 years ago

As I pointed out in my comment on issue 12 this callback is invoked for successful sends as well.