Skip to content

useWebSocket | Support changing heartbeat intervals #5105

@sharkked

Description

@sharkked

Clear and concise description of the problem

I love this composable! But for it to be viable for my use case, I need to be able to adjust the heartbeat period in useWebSocket so that an incoming message can adjust the timing of each pulse. Specifically, I have a server which sends a "welcome" message including the heartbeat interval after the client has connected and identified. The function currently accepts a raw number which cannot be modified after initializing the composable, and the interval is not in any way exposed to the user.

Suggested solution

Given that the present implementation of useWebSocket already uses useIntervalFn which defines interval as a MaybeRefOrGetter<number>, I would expect this to be as trivial as adjusting the type of heartbeat.interval accordingly. I will submit a PR for this once I can check.

Alternative

I see a couple more relevant opportunities for improved fine-grained control of the composable.

  • Heartbeat useIntervalFn returns are not at all exposed to the user. Could potentially return i.e. heartbeat = { pause, resume, isActive } when present, allowing pausing the heartbeat if necessary
  • In some situations, I would also like to adjust the delay between reconnect attempts (e.g., increasing the delay with each subsequent failed attempt). At the moment, this is a setTimeout with a static delay. Could potentially pass this value as a function of retries i.e. number | ((retried: number) => number). Enhancement of useWebSocket | Provide current retry count parameter to to the autoReconnect.retries #4603

Additional context

There is an existing issue #4671 regarding the introduction of a new web worker-based timing function, which also utilizes MaybeRefOrGetter<number> as the interval.

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions