Indeed, event listeners are not obvious in Photoshop. Probably the best approach for listeners in Photoshop is to make use of the Notifier(s) event-handler object. See the Photoshop Javascript Reference for more information on usage.
Typically, you create Notifiers using File > Scripts > Scripts Event Manager, where you can choose the "Close Document" Photoshop event and choose a script or an action to run when it happens.
But, if you are creating an extension and need to do this via code, it's a little trickier. Your best bet is to create a Notifier for the close event:
var scriptPath = "/c/scripts/server.jsx";
//note the trailing space in "Cls "
var closeEventNotifierRef = app.notifiers.add("Cls ", File(scriptPath));
Add whatever needs to happen with the server in that server.jsx file. Note that the file will only be called after the document is closed, so I am not sure what info will still be accessible.
Also note that, as far as I know, adding notifiers is persistent, meaning that close notifier and the calling of server.jsx will happen every time after. In other words, the close event notifier will not be deleted once your server.jsx script finishes executing.
Because of this, you may want to add logic inside your server.jsx file to take care of removing the close event notifier at the end. However, this is not as simple, since you no longer have access to the notifier reference created (closeEventNotifierRef). The only way I found was to loop through notifiers:
var notifiersAll = app.notifiers;
var notifierRefs = [];
for (var i = 0; i < notifiersAll.length; i++){
if (notifiersAll[i].event == 'Cls ') {
notifierRefs.push(notifiersAll[i]);
}
}
for (var r = 0; r < notifierRefs.length; r++){
notifierRefs[r].remove();
}
Hope this helps.