http - Is there a way to be notified when a client has unsubscribe from server sent events? -
as understand when request event emitter on server arrives, request never closed , need res.write()
every time send message. there way notified when client performed request has left? there property on request
object?
suppose have following route
app.get('/event',function(req,res){ //set response headers //how check if req object still active send message , perform other actions? })
the basic sequence of events should similar in other frameworks, example grails 3.3.
first set endpoints subscribe, , close connection.
def index() { // handler /api/subscribe rx.stream { observer observer -> // grails event bus. background tasks, // services , other controllers can post these // events, client_hangup, send_msg, // string constants. eventbus.subscribe(client_hangup) {string msg -> // code handle when grails event bus // posts client_hangup // side effects here, update counter // close sse connection observer.oncompleted() return } eventbus.subscribe(send_msg) {string msg -> // send server sent event observer.onnext(rx.respond(msg)) } } } def disconnecting() { // handler /api/disconnect // post client_hangup event grails event bus notify(client_hangup, 'disconnect') }
now in client, need arrange get /api/disconnect
whenever use-case requires it. assuming want notice when navigates away page, register function on window.onbeforeunload
. example using vue.js
, axios
.
window.onbeforeunload = function (e) { e.preventdefault() vue.$http({ method: 'get', url: 'http://localhost:8080/api/disconnect' }) .then((response) => { console.log(response) }) .catch(({error}) => { console.log(error) }) }
in case of servlet stacks grails, found needed if had no housekeeping of own when browser went away. without it, page reloads causing ioexceptions on end.
Comments
Post a Comment