hapijs / nes

WebSocket adapter plugin for hapi routes
Other
502 stars 87 forks source link

Can't received publish messages from browser #326

Closed sjzamora86 closed 1 year ago

sjzamora86 commented 1 year ago

Support plan

Context

How can we help?

Hi,

This issue record, not actually an issue anymore because I managed to find the fix. I raised this for those people in case have the same issue since I can't find a relevant posting here.

My simple application try the subscription pattern which you can see below:

Server - I used Glue to register Nes to my application and set the auth = false for testing purpose only

   const server = await Glue.compose({
         host: 0.0.0.0,
        port: 7272,
   },
   register: {
        {
                plugin: require('@hapi/nes'),
                options: {
                    auth: false
                }
            }
   }
);

    server.subscription('/item/{id}');
    await server.start();

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

Client - different from the example my client is from a React app and subscribing on browser

import {Client} from '@hapi/nes/lib/client';

    const start = async () => {
    try {
       const client = new Client('ws://localhost');
        await client.connect();

       const updateHandler = (update, flags) => {
            console.log('update handler => ', update, flags);
           // update handler => { id: 5, status: 'complete' }
           // Second publish is not received (doesn't match)
        };

        client.subscribe('/item/5', updateHandler);

    } catch (e) {
        console.error(e);
    }
};

Issue: The issue I can't received the publish messages but when I tried broadcasting message I can received it on browser

Fixed: After checking the documentation, I saw that the server.subscription has an options argument that we can set auth. After setting auth to false on subscription method and match the registration config, the browser starting to get the messages when I publish

server.subscription('/item/{id}', {
          auth: false
});
sjzamora86 commented 1 year ago

As said on my issue above - this is fixed and just want to raise this to allow other people to see if ever they experience the same.

Nargonath commented 1 year ago

Thank you for sharing your solution @sjzamora86. I'm glad you found a solution to your problem.