Redirect to a new page in response to a Socket.IO event

I am developing a multi-page web application with Express that has chat features (powered by Socket.IO). Users can invite others to a private shared chat (which also has other components, such as a shared drawing board). I would like to redirect both users (invitation and invitation) to a different URL after the completion of the invitation transaction.

Since socket.io does not have access to the request and response objects of my routes, I tried passing my socket.io instance to my route processing. Problems with duplicate event handlers accumulate when the page is refreshed, Express throws an error when trying to redirect after the response has already been sent (this makes sense).

It seems that the only way to achieve this is to redirect to the client, and the new chat URL is passed to each of Socket.IO. However, relying on a client for government transfer seems too fragile.

Are there any other options? What is the best way to implement what I need?

+3
source share
1 answer

I'm not sure if it will be too fragile to do in the client. Just send the url via socket and execute window.location.href = redirectUrlor $('div').load(newContentUrl)using jQuery.

0
source

All Articles