Skip to content

Commit

Permalink
notificationClient: Establish ws connection only on client render
Browse files Browse the repository at this point in the history
The current websocket configuration, creates a new websocket client, for every SSR generated page. Since those clients are technically active, they are never cleaned up, thus resulting in memory leak.
This change makes it so, a new web socket connection is created only on first render of the component.

Tests:
1. Start visiting different pages randomly
2. Observe the current websocket clients, in the Socket object. They can be found in Socket.adapters.sids
3. The size of sids should remain 1, per browser window
  • Loading branch information
sashko9807 committed Jul 1, 2023
1 parent cd8a9ed commit 85a80b0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/common/util/notificationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { io } from 'socket.io-client'
const { publicRuntimeConfig } = getConfig()
const notificationClient = io(publicRuntimeConfig.API_URL, {
transports: ['websocket'],
autoConnect: false,
})

export default notificationClient
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function NotificationSnackBar({
const [notifications, setNotifications] = useState<NotificationLayoutData[]>([])

useEffect(() => {
if (!notificationClient.connected) notificationClient.connect()
notificationClient.on('successfulDonation', (notificationData: NotificationLayoutData) => {
setNotifications((prevState) => [...prevState, notificationData])
})
Expand Down

0 comments on commit 85a80b0

Please sign in to comment.