It really depends on what you want to achieve in your flow.
If the user closes the webview with close-x (top right), you should think of it as a normal closing event and not process anything at all. Instead, you should use a special save / confirm and / or close button in your webview.
Then, the close button launches the requestCloseBrowser function of the Messenger Extension SDK, and the save / confirm button handles the actual save / confirm / process action.
If you really need to find the user closing the webview with close-x in the upper right corner, this can only be achieved by a long poll (polling a user session from your webview).
source share