Skip to content
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

WebSockets #508

Merged
merged 9 commits into from
Sep 3, 2024
Merged

Conversation

lukaskabc
Copy link
Collaborator

Implementation of WebSockets to address changes in kbss-cvut/termit#290

React-Scripts & Create React App Configuration Override

React-Scripts does not allow the modification of webpack configuration, which was required to support .mjs files required by a new library SvenKirschbaum/react-stomp-hooks.

To add an option to override some configurations, Craco was added as a dev dependency.

Changed dependencies

Some package versions were adjusted to address audit security issues.

Changes

  • App was wrapped in WebSocket provider - note that it is required for this wrapper to be above others, for example, above React router, to keep the continuous WebSocket connection.
  • ActionType was changed from constant to enum to allow usage of actions as typed keys
  • fixed the dispatches vocabulary term count loading action on success test, which seemed to be wrong
  • BrowserStorage will now artificially fire storage events to allow listening of token change (same for SecurityUtils when the token is cleared)

WebSocket handling

WebSocket connection is established automatically by WebSocketWrapper when a security token is available (not blank), and the lost connection is automatically reconnected.

Publishing

The useStompClient() hook can be used to publish a message from a component, or the withStompClient wrapper can be used for HOC.

Subscribing

The component can subscribe to messages during its lifetime with the useSubscription hook or the withSubscription wrapper for HOC.

Subscribing in a component has a disadvantage - the subscription is canceled and renewed every time React decides to remount the element, so the subscription may eventually miss some message (and BE is notified about un/subscribing every time too).

As an alternative, it is possible to register a global WebSocketDispatcher, which will be active for the life-cycle of WebSocketWrapper.
Global dispatchers are registered in src/reducer/WebSocketDispatchers.ts#DISPATCHERS constant for individual actions using a helper function to construct the key-value pair.
Dispatcher is defined by subscribed destinations, relevant action, and the message callback.
Each dispatcher may listen to one or more destinations.

All registered dispatchers are instanced by WebSocketApp asWebSocketDispatcherSubscriber components.

@lukaskabc lukaskabc requested a review from ledsoft September 3, 2024 09:40
Copy link
Contributor

@ledsoft ledsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, will wait with merge for backend.

@ledsoft ledsoft merged commit 48c4a6b into kbss-cvut:development Sep 3, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants