Skip to content

Socket closes when tab is in background – is ping/pong paused? #273

Description

@yasmanets

Hi! I'm experiencing an issue where the WebSocket connection gets closed when the browser tab is left in the background for a while.

I’m using the following config:

const { sendMessage } = useWebSocket(socketUrl,
	{
		heartbeat: {
			message: encodeMsg({ action: 'ping' }),
			returnMessage: `pong`,
			timeout: 3000,
			interval: 500
		},
		shouldReconnect: () => {
			return true;
		},
		reconnectInterval: 300,
		reconnectAttempts: 10
	},
);

What happens:

When I leave the tab inactive (e.g., switch to another tab or minimize the browser), after some time the WebSocket connection closes.
This seems to happen because the ping/pong messages stop being sent or received.
When I return to the tab, a new socket is created, triggering a reconnection.

My question:

Does react-use-websocket pause activity (like sending/receiving ping/pong or timers) when the tab is in the background or suspended by the browser?

If so, is there a recommended way to keep the connection alive or resume it properly once the tab becomes active again?

Thanks in advance!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions