Giter Site home page Giter Site logo

Cannot get pub/sub example to work about nes HOT 7 CLOSED

rgov avatar rgov commented on August 30, 2024
Cannot get pub/sub example to work

from nes.

Comments (7)

rgov avatar rgov commented on August 30, 2024 3

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?

from nes.

nazreen avatar nazreen commented on August 30, 2024

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

from nes.

hueniverse avatar hueniverse commented on August 30, 2024

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.

from nes.

nazreen avatar nazreen commented on August 30, 2024

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.

from nes.

rgov avatar rgov commented on August 30, 2024

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.

from nes.

nazreen avatar nazreen commented on August 30, 2024

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.

from nes.

devinivy avatar devinivy commented on August 30, 2024

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' });
})();

from nes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.