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

Properly Shutdown pusher-js client to ensure any pending messages are sent. #669

Open
jamesrusso opened this issue Dec 15, 2022 · 11 comments

Comments

@jamesrusso
Copy link

Do you want to request a feature or report a bug?

Feature/Clarification

What is the current behavior?

There is no clear way to shutdown the pusher-js client while waiting for any pending messages to be emitted and delivered to the server. We are using private channels to allow some backend process exchange messages with a web front end. However, when we send our final message indicating sign-off and then exit the node process, often the message is not received by the remote agent, I assume because the node process has exited before the message could be sent.

If the current behavior is a bug, please provide the steps to reproduce and
if possible a minimal demo of the problem via https://jsfiddle.net or similar.

I can try and work on a jsfiddle example and will update the ticket.

What is the expected behavior?

I would expect there be some type of promise which could be awaited to shutdown the pusher-js client to ensure delivery of any pending messages (and prevent queuing of additional messages). Some type of wind down functionality.

Which versions of Pusher, and which browsers / OS are affected by this issue?
Did this work in previous versions of Pusher? If so, which?

7.4.0

@benw-pusher
Copy link
Contributor

Can you clarify where the events originate here? My understanding is that the flow is as follows:
Node.JS server is sending events to your JS client
JS client receives events as expected
Node.JS sends final sign-off event
Node.JS shuts down
Client does not receive the sign-off event.

Is this correct?

@jamesrusso
Copy link
Author

It's actually node processes, process Client and Process Server using private channel..

  1. Server -> Client: SESSION_COMPLETE
  2. Server is now WAITING for SESSION_COMPLETE_ACK from Client.
  3. Client -> Server: SESSION_COMPLETE_ACK
  4. Client: calls process.exit(0)
  5. Server: Times out waiting for SESSION_COMPLETE_ACK since it never arrives after being sent in Step 2.

@benw-pusher
Copy link
Contributor

Thanks, and can you confirm how the ack is sent back to the server?

@jamesrusso
Copy link
Author

jamesrusso commented Dec 16, 2022

I'll spend some time in the console checking to see if the message arrives. I suspect that the process.exit(0) immediately after emitting the message causes it to never be sent, but I'll try and confirm in the console.

Edit: I assume that you mean to the "pusher" server, and not the server in the example. I suspect it is never arriving at the pusher server, because otherwise there would be no reason for it to NOT be delivered to our server process waiting for the ACK.

@benw-pusher
Copy link
Contributor

Sorry for not being clear - I was referring to how this event is sent:
Client -> Server: SESSION_COMPLETE_ACK

@jamesrusso
Copy link
Author

Here is the server sending the SESSION_COMPLETE and awaiting the SESSION_COMPLETE_ACK.

  async sessionComplete(result: SessionResult, options: WaitOptions = DefaultWaitOptions) {
    this.cancelHeartbeats();
    const ackPromise = this.waitForEvent(EventType.SESSION_COMPLETE_ACK, options);
    this.dispatchEvent(
      EventType.SESSION_COMPLETE,
      result,
    );
    await ackPromise;
    await this.disconnectSession();
  }

Where dispatchEvent just calls:

return this.channel.trigger(eventType, eventData);

Then in the client we register a handler for the SESSION_COMPLETE message and trigger the ACK..

  /**
   * Register a function to be called when the session is completed.
   * 
   * @param cb The callback to receive the notification that the session is completed. 
   */
  async onSessionCompletion(cb: SessionCompletionCallback) {
    this.registerEvent(EventType.SESSION_COMPLETE, async (result: SessionResult)=>{
      this.cancelHeartbeats()
      this.dispatchEvent(EventType.SESSION_COMPLETE_ACK, {})
      await this.disconnectSession()
      cb(result)})
  }

@stale
Copy link

stale bot commented Mar 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.

@stale stale bot added the wontfix label Mar 18, 2023
@benw-pusher
Copy link
Contributor

Just to update here, this has been raised internally however at this time changes are not being prioritised in the roadmap.

@jamesrusso
Copy link
Author

@benw-pusher Thanks for providing the update.

@stale
Copy link

stale bot commented Aug 10, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.

@stale stale bot added the wontfix label Aug 10, 2023
@benw-pusher
Copy link
Contributor

Preventing closure - this remains outstanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants