-
Notifications
You must be signed in to change notification settings - Fork 28
Implement reconnect and retry when web socket is disconnected #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d260f6
implement reconnection functionality for websocket
nassery318 7e915fe
clean up
nassery318 230ab74
renaming
nassery318 f2514e7
replace toast retry and reconnect with a custom reconnect page and fu…
nassery318 41b269d
Merge branch 'main' into implement-reconnect-and-retry
nassery318 0faff4a
web socket reconnect behaviour fixed
nassery318 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { useEffect } from "react" | ||
| import { useDispatch, useSelector } from "react-redux" | ||
| import { useNavigate } from "react-router" | ||
| import { CONNECTED, CONNECTING, ERROR } from "@common/src/constants" | ||
| import { Loader2, WifiOff, AlertCircle, ServerOff } from "lucide-react" | ||
| import type { RootState } from "@/store" | ||
| import { connectPending } from "@/state/wsconnection/wsConnectionSlice" | ||
|
|
||
| export function Reconnect() { | ||
| const dispatch = useDispatch() | ||
| const navigate = useNavigate() | ||
| const wsConnection = useSelector((state: RootState) => state.websocket) | ||
| const { status, reconnect, errorMessage } = wsConnection | ||
|
|
||
| useEffect(() => { | ||
| // redirect to previous location on successful connection | ||
| if (status === CONNECTED) { | ||
| const redirectTo = sessionStorage.getItem("previousLocation") || "/connect" | ||
| sessionStorage.removeItem("previousLocation") | ||
| navigate(redirectTo, { replace: true }) | ||
| } | ||
| }, [status, navigate]) | ||
|
|
||
| const handleManualReconnect = () => { | ||
| dispatch(connectPending()) | ||
| } | ||
|
|
||
| const getProgressPercentage = () => { | ||
| if (reconnect.maxRetries === 0) return 0 | ||
| return ((reconnect.currentAttempt) / reconnect.maxRetries) * 100 | ||
| } | ||
|
|
||
| const getNextRetrySeconds = () => { | ||
| if (!reconnect.nextRetryDelay) return 0 | ||
| return Math.ceil(reconnect.nextRetryDelay / 1000) | ||
| } | ||
|
|
||
| const isExhausted = status === ERROR && !reconnect.isRetrying | ||
|
|
||
| return ( | ||
| <div className="flex items-center justify-center min-h-screen dark:from-gray-900 dark:to-gray-800"> | ||
| <div className="w-full max-w-md p-8 space-y-6 bg-white dark:bg-gray-800 rounded-lg shadow-xl"> | ||
| <div className="flex justify-center"> | ||
| {status === CONNECTING && reconnect.isRetrying ? ( | ||
| <div className="relative"> | ||
| <Loader2 className="w-16 h-16 text-tw-primary animate-spin" /> | ||
| </div> | ||
| ) : isExhausted ? ( | ||
| <ServerOff className="w-12 h-12 text-red-500" /> | ||
| ) : ( | ||
| <div className="p-4 bg-gray-100 dark:bg-gray-700 rounded-full"> | ||
| <WifiOff className="w-12 h-12 text-gray-500" /> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| <div className="text-center space-y-2"> | ||
| <h1 className="text-2xl font-bold text-gray-900 dark:text-white"> | ||
| {isExhausted | ||
| ? "WebSocket Server Disconnected" | ||
| : "Reconnecting to Server..."} | ||
| </h1> | ||
| <p className="text-sm text-gray-600 dark:text-gray-400"> | ||
| {isExhausted | ||
| ? "Unable to connect to the WebSocket server" | ||
| : "Attempting to restore connection to the WebSocket server"} | ||
| </p> | ||
| {errorMessage && ( | ||
| <div className="mt-2 p-3 bg-red-50 dark:bg-red-900/20 rounded-md"> | ||
| <div className="flex items-start gap-2"> | ||
| <AlertCircle className="w-4 h-4 text-red-500 mt-0.5 flex-shrink-0" /> | ||
| <p className="text-sm text-red-700 dark:text-red-400 text-left"> | ||
| {errorMessage} | ||
| </p> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| {/* Retry Progress */} | ||
| {reconnect.isRetrying && ( | ||
| <div className="space-y-3"> | ||
| <div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2 overflow-hidden"> | ||
| <div | ||
| className="bg-tw-primary h-full transition-all duration-300 ease-out" | ||
| style={{ width: `${getProgressPercentage()}%` }} | ||
| /> | ||
| </div> | ||
|
|
||
| {/* Retry Information */} | ||
| <div className="flex justify-between text-sm"> | ||
| <span className="text-gray-600 dark:text-gray-400"> | ||
| Attempt {reconnect.currentAttempt} of {reconnect.maxRetries} | ||
| </span> | ||
| {reconnect.nextRetryDelay && ( | ||
| <span className="text-gray-600 dark:text-gray-400"> | ||
| Next retry in {getNextRetrySeconds()}s | ||
| </span> | ||
| )} | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| {isExhausted && ( | ||
| <div className="space-y-2 pt-4"> | ||
| <button | ||
| className="w-full border p-2 rounded bg-tw-primary text-white hover:bg-tw-primary/80 cursor-pointer transition-colors" | ||
| onClick={handleManualReconnect} | ||
| > | ||
| Try Reconnecting | ||
| </button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| import { useSelector } from "react-redux" | ||
| import { useParams } from "react-router" | ||
| import { CONNECTED, DISCONNECTED } from "@common/src/constants.ts" | ||
| import { CONNECTED, CONNECTING, DISCONNECTED } from "@common/src/constants.ts" | ||
| import { selectStatus } from "@/state/valkey-features/connection/connectionSelectors.ts" | ||
|
|
||
| const useIsConnected = (): boolean => { | ||
| const { id } = useParams<{ id: string }>() | ||
| const status = useSelector(selectStatus(id!)) | ||
| // user will stay in the page if connected or disconnected | ||
| return status === CONNECTED || status === DISCONNECTED | ||
| return status === CONNECTED || status === DISCONNECTED || status === CONNECTING | ||
| } | ||
|
|
||
| export default useIsConnected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { useEffect, useRef } from "react" | ||
| import { useSelector } from "react-redux" | ||
| import { useLocation, useNavigate } from "react-router" | ||
| import { CONNECTED, CONNECTING, ERROR } from "@common/src/constants" | ||
| import type { RootState } from "@/store" | ||
|
|
||
| export function useWebSocketNavigation() { | ||
| const navigate = useNavigate() | ||
| const location = useLocation() | ||
| const wsConnection = useSelector((state: RootState) => state.websocket) | ||
| const previousStatus = useRef(wsConnection.status) | ||
|
|
||
| useEffect(() => { | ||
| const currentStatus = wsConnection.status | ||
| const wasConnected = previousStatus.current === CONNECTED | ||
| const isNowDisconnected = currentStatus === CONNECTING && wsConnection.reconnect.isRetrying | ||
| const isReconnecting = location.pathname === "/reconnect" | ||
|
|
||
| if (isReconnecting || location.pathname === "/connect") { | ||
| previousStatus.current = currentStatus | ||
| return | ||
| } | ||
|
|
||
| if (wasConnected && isNowDisconnected) { | ||
| // store location and navigate to reconnecting | ||
| sessionStorage.setItem("previousLocation", location.pathname) | ||
| navigate("/reconnect", { replace: true }) | ||
| } | ||
|
|
||
| if (currentStatus === ERROR && !wsConnection.reconnect.isRetrying && !isReconnecting) { | ||
| sessionStorage.setItem("previousLocation", location.pathname) | ||
| navigate("/reconnect", { replace: true }) | ||
| } | ||
|
|
||
| previousStatus.current = currentStatus | ||
| }, [wsConnection.status, wsConnection.reconnect.isRetrying, location.pathname, navigate]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would implement that in epic with chain/flatmap but since it's a single isolated hook, it's fine too