pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.23k stars 199 forks source link

"beforeReconnect" & "afterReconnect" callbacks #28

Closed bennycode closed 6 years ago

bennycode commented 7 years ago

We have a use case where we need to execute a function before every WebSocket reconnect. It would be great if the ReconnectingWebsocket instance could execute a function (like beforeReconnect) on every reconnect.

Another option, to fulfill our needs, would be making use of Promises for getUrl. But this requires the ReconnectingWebsocket to wait for a Promise to resolve.

Idea:

const getUrl = () => Promise.resolve('ws://echo.websocket.org/?id=something_special');
const rws = new ReconnectingWebSocket(getUrl);
pladaria commented 7 years ago

I like the idea of using a Promise for getUrl (as addition to other options like string or function)

hhamalai commented 7 years ago

I've been trying to implement async flow here too, however registering event listeners in this current manner gets complicated requiring the event handlers to be passed into ReconnectingWebSocket as parameters.

pladaria commented 6 years ago

Latest version supports passing () => Promise<string> as url parameter