diff --git a/client/src/App.jsx b/client/src/App.jsx
index b45dc7151..bbe919b29 100644
--- a/client/src/App.jsx
+++ b/client/src/App.jsx
@@ -6,23 +6,26 @@ import { PresenterMessageProvider } from './app/context/presenterMessageContext'
import Editor from './features/editors/Editor';
import DefaultPresenter from './features/viewers/DefaultPresenter';
import { QueryClient, QueryClientProvider } from 'react-query';
+import SocketProvider from './app/context/socketContext';
const queryClient = new QueryClient();
function App() {
return (
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
);
}
diff --git a/client/src/app/context/socketContext.js b/client/src/app/context/socketContext.js
new file mode 100644
index 000000000..842cdd02b
--- /dev/null
+++ b/client/src/app/context/socketContext.js
@@ -0,0 +1,26 @@
+import { createContext, useContext, useEffect, useState } from 'react';
+import io from 'socket.io-client';
+import { serverURL } from '../api/apiConstants';
+
+export const SocketContext = createContext([[], () => {}]);
+
+export const useSocket = () => {
+ return useContext(SocketContext);
+};
+
+function SocketProvider(props) {
+ const [socket, setSocket] = useState();
+
+ useEffect(() => {
+ const socket = io(serverURL);
+ setSocket(socket);
+ return () => socket.disconnect();
+ }, []);
+
+ return (
+
+ {props.children}
+
+ );
+}
+export default SocketProvider;
diff --git a/client/src/features/control/PlaybackControl.jsx b/client/src/features/control/PlaybackControl.jsx
index 5407e0ce2..755c99555 100644
--- a/client/src/features/control/PlaybackControl.jsx
+++ b/client/src/features/control/PlaybackControl.jsx
@@ -11,8 +11,6 @@ import {
import { format } from 'date-fns';
import { timeFormatSeconds } from '../../common/dateConfig';
import { useEffect, useState } from 'react';
-import { io } from 'socket.io-client';
-import { serverURL } from '../../app/api/apiConstants';
import {
getStart,
getPause,
@@ -20,6 +18,7 @@ import {
getPrevious,
getNext,
} from '../../app/api/playbackApi';
+import { useSocket } from '../../app/context/socketContext';
// BUTTON DEFINITION
const defProps = {
@@ -33,28 +32,24 @@ const size = {
export default function PlaybackControl(props) {
const [playback, setPlayback] = useState(null);
+ const socket = useSocket();
const [timer, setTimer] = useState({
currentSeconds: null,
startedAt: null,
expectedFinish: null,
});
- const updateTimer = (vals) => {
- setTimer({ ...timer, ...vals });
- };
- // WEBSOCKETZ
useEffect(() => {
- // TODO: add namespace?
- const socket = io(serverURL, { transport: ['websocket'] });
- console.log('websocket started');
+ if (socket == null) return;
// Handle timer
socket.on('timer', (data) => {
- updateTimer(data);
+ setTimer({ ...data });
});
- return () => socket.disconnect();
- }, []);
+ return () => socket.off('timer');
+ }, [socket]);
+
const playbackControl = async (action, payload) => {
switch (action) {
@@ -83,10 +78,10 @@ export default function PlaybackControl(props) {
}
};
- const started = timer.startedAt
+ const started = timer?.startedAt
? format(timer.startedAt, timeFormatSeconds)
: '...';
- const finish = timer.expectedFinish
+ const finish = timer?.expectedFinish
? format(timer.expectedFinish, timeFormatSeconds)
: '...';
@@ -94,7 +89,7 @@ export default function PlaybackControl(props) {