socketcluster icon indicating copy to clipboard operation
socketcluster copied to clipboard

Publish does not fail when there are no sockets subscribed to channel.

Open sylvesterdsouza opened this issue 6 years ago • 6 comments

I am using the below code within worker.js to allow a back end PHP script to push messages to clients.

The err function never executes even when there are no sockets subscribed to the channel.

Is there some way to detect when there are no active subscriptions to a channel?

app.post('/push', (request, response) => {
    const postBody = request.body;
    console.log("Received push message for : " + postBody['channelid'] + " From : " + request.connection.remoteAddress);
    var message = new Object();
    message.type="alert";
    message.data=postBody['messagedata'];
    scServer.exchange.publish(postBody['channelid'],message, function (err) {
        if (err) {
        // Failed to publish event, retry or let the user know and keep going?
        console.log("Push Failed");
        response.sendStatus(500)
        response.end();
    } else {
        // Event was published successfully
        console.log("Push Sent to Channel");
        response.sendStatus(200)
        response.end();
    }
    });
});

sylvesterdsouza avatar Jun 09 '19 14:06 sylvesterdsouza

I was wondering the exact same thing. I'd like to implement a logic for dynamically handling publishing of data. Check if a specific channel (e.g. private channel of a user) is subscribed, if so - send the data via the connected channel. If however there's no active subscription for this channel available, send a specfic push notification instead (since we can assume the user doesn't have an active session and is therefore not using the App.)

@sylvesterdsouza did you make any progress on this?

KochMario avatar Feb 10 '21 14:02 KochMario

Unfortunately no such luck, we ended up just pushing the notifications since they were time bound and the user would miss it if they were not logged in anyway.

On Wed, Feb 10, 2021 at 2:59 PM Mario Koch [email protected] wrote:

I was wondering the exact same thing. I'd like to implement a logic for dynamically handling publishing of data. Check if a specific channel (e.g. private channel of a user) is subscribed, if so - send the data via the connected channel. If however there's no active subscription for this channel available, send a specfic push notification instead (since we can assume the user doesn't have an active session and is therefore not using the App.)

@sylvesterdsouza https://github.com/sylvesterdsouza did you make any progress on this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/SocketCluster/socketcluster/issues/500#issuecomment-776765706, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ6PIVKMRYYPZF6JXFJ4Q3S6KNNNANCNFSM4HWJSOWA .

-- Sylvester D'souza | Consultant - Systems Architecture | m: + 91 750 799 9369 | e: [email protected] | Address : A-2 Sapana Gardens, Chogm Road, Alto Porvorim, Bardez Goa 403521

sylvesterdsouza avatar Feb 11 '21 08:02 sylvesterdsouza

Okay I see. Thanks for the fast reply!

I might implement a logic in which I tag the Push Notification recipients when they've an active session and then filter based upon that when sending Push Notifications. Have you considered that as well? Socket will deliver to all users that are subscribed to that channel. Push Notification will send only if the target users have no active session.

Gotta test if that reliably works though. Since that would basically be at most once guarantee. If certain users wouldn't get any notification at all with that system it might be better to switch to at least once and send it regardless if they've an active session or not 🤔

KochMario avatar Feb 11 '21 09:02 KochMario

With tabbed browsers users can have active sessions even after they close the tab up until the servers session timeout value is reached ( the session timeout was quite large in our app )

On 11-Feb-2021, at 2:44 PM, Mario Koch [email protected] wrote:

 Okay I see. Thanks for the fast reply!

I might implement a logic in which I tag the Push Notification recipients when they've an active session and then filter based upon that when sending Push Notifications. Have you considered that as well? Socket will deliver to all users that are subscribed to that channel. Push Notification will send only if the target users have no active session.

Gotta test if that reliably works though. Since that would basically be at most once guarantee. If certain users wouldn't get any notification at all with that system it might be better to switch to at least once and send it regardless if they've an active session or not 🤔

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sylvesterdsouza avatar Feb 11 '21 09:02 sylvesterdsouza

Yeah good point. I'd trigger it manually though. Once the App goes into inactive mode I manually set a flag. Since this event is instant and not attached to any session timeout that should work - in theory.

KochMario avatar Feb 11 '21 13:02 KochMario

Publish can only throw an error if the message failed to reach the server, it doesn't count last mile delivery to individual subscribers... If that were the case, publish operations would always fail every single time because when you have thousands of subscribers, there will always be one user who has bad internet... Nothing the publisher can do about that.

If you want to establish a delivery guarantees, you just need to setup one or more channels named after the user and the user can publish some kind of 'receipt' message containing the original message's UUID whenever they receive a pub/sub message from some other channel. This approach is the most scalable.

jondubois avatar Feb 11 '21 14:02 jondubois