Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get pub/sub example to work #309

Closed
rgov opened this issue May 7, 2020 · 7 comments
Closed

Cannot get pub/sub example to work #309

rgov opened this issue May 7, 2020 · 7 comments
Labels
support Questions, discussions, and general support

Comments

@rgov
Copy link

rgov commented May 7, 2020

Support plan

  • which support plan is this issue covered by?: Community
  • is this issue currently blocking your project?: yes
  • is this issue affecting a production system?: no

Context

  • node version: 13.12.0
  • module version: 12.0.2
  • environment: node
  • used with: hapi 19.1.1
  • any other relevant information:

How can we help?

I'm just trying to run the subscription sample code here.

If I use the code unmodified, I get from the client:

(node:26349) UnhandledPromiseRejectionWarning: Error: Connection terminated while waiting to connect

Perhaps the default port is 80, and I'm not running as root, but then I'd expect the server script to tell me it cannot bind to the desired port? It says nothing.

Anyway, if I modify the server and client to specify a host and port, then the connection succeeds:

const server = new Hapi.Server({ address: "localhost", port: 1555 });
const client = new Nes.Client('ws://localhost:1555');

The client example doesn't do anything when a message is received, so I added a console.log("foo") call to it.

Yet the callback never seems to get fired. Is something missing from the examples or my setup?

I know that the server is getting the subscription because I can add an onSubscribe callback. If I add a filter callback it doesn't appear to be called. I don't know if there is a race condition where the messages are published before the client connects or something, but I also tried sending a message in the onSubscribe callback and that didn't work either.

For what it's worth, the route invocation example code works fine (with port change).

@rgov rgov added the support Questions, discussions, and general support label May 7, 2020
@rgov
Copy link
Author

rgov commented May 7, 2020

I might be correct about the race condition issue. If I change it to:

    setInterval(async () => {
        await server.publish('/item/5', { id: 5, status: 'complete' });
    }, 1000);

Then messages go through, the filter callback fires, the client's handler fires, etc.

Can the example be updated to something that works properly?

@nazreen
Copy link

nazreen commented Jun 17, 2020

@rgov in my own development, I was stumped for 15 minutes simply because I was using nodemon for both server and client and thus the client was trying to connect when the connection has not been established yet. restarting the client after the server is up works. this point should be mentioned in the example.

@hueniverse
Copy link
Contributor

Unfortunately, no community resources were available to help resolve this issue after two weeks, and it is being closed. We close unresolved community issues to keep the issue tracker organized and effective. Please check https://hapi.dev/support for other support options.

@nazreen
Copy link

nazreen commented Jul 10, 2020

the issue can be fixed by clarifying in the docs that the server and client shouldn't be in the same file or started at the same time, which is a 'no brainer' when you think about it, but is very easily overlooked when you're following docs and trying to get something to work the first time.

@rgov
Copy link
Author

rgov commented Jul 10, 2020

I don't think they were in the same file, but it's been a while. That said I'm not sure why it should matter if they're in separate files, it's supposed to support concurrency, right?

Re-reading my bug report, I think there are at least 3 things wrong with the example (and/or the module) from my post.

It makes sense that the client can only connect after the server is listening, of course, but the fact that publishing a message didn't work in onSubscribe but did after a 5 second timer suggests something weird about what's going on behind the scenes.

@nazreen
Copy link

nazreen commented Jul 10, 2020

yes it's not the fact that it's in the same file that's the issue, just that starting up the client in the line right after starting the server will most likely result in this issue since not enough time has passed since the server was started.

was it 5 seconds? your comment above has 1000 miliseconds, which was what I was able to replicate too.

@devinivy
Copy link
Member

Here is a working example that doesn't rely on a race. In short, you should start the server before connecting the client, and let the subscription complete before having the server publish.

const Hapi = require('@hapi/hapi');
const Nes = require('@hapi/nes');

(async () => {

    const server = new Hapi.Server();
    const client = new Nes.Client('ws://localhost');

    await server.register(Nes);
    server.subscription('/item/{id}');
    
    await server.start();
    await client.connect();

    const handler = (update, flags) => {

        // update -> { id: 5, status: 'complete' }
        // Second publish is not received (doesn't match)
    };

    await client.subscribe('/item/5', handler);

    server.publish('/item/5', { id: 5, status: 'complete' });
    server.publish('/item/6', { id: 6, status: 'initial' });
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

4 participants