lukeed / sockette

The cutest little WebSocket wrapper! 🧦
MIT License
2.45k stars 81 forks source link

Update url param to be dynamic if needed #65

Open Gabiras12 opened 3 years ago

Gabiras12 commented 3 years ago

As requested by issue #59 my use case is the same. I need to pass a token on every connection attempt and it's only possible to use the token once. So the url must change on every reconnect attempt.

If url param is a function invoke it passing the attempt number

Solution: Change the url param to be of type:

- string
- Promise<string>
- (attempt: number) => Promise<string>
- (attempt: number) => string

So the usage would look like:

// string as it is now
new Sockette('wss://address')
// promise style. just adding the setTimeout to simulate
new Sockette(new Promise((resolve) => {
  setTimeout(() => {
    resolve('wss://address')
  }, 5000)
}))

new Sockette(Promise.resolve('wss://address'))

new Sockette((attempt) => `wss://address?attempt${attempt}`)

new Sockette(async (attempt) => await Promise.resolve(`wss://address?attempt${attempt}`))
Gabiras12 commented 3 years ago

@lukeed can we have this merged?

ghost commented 3 years ago

+1 to @Gabiras12 @lukeed let us know if this PR could be merged and released.

exaucae commented 2 years ago

bump!

Gabiras12 commented 2 years ago

@ghost and @exaucae I've published this PR under sockette-dynamic-url https://www.npmjs.com/package/sockette-dynamic-url