Skip to content

Commit

Permalink
adding realtime connection functionality to document view
Browse files Browse the repository at this point in the history
  • Loading branch information
mbogo-mit committed Dec 26, 2023
1 parent e549b2b commit e688c58
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 50 deletions.
3 changes: 3 additions & 0 deletions src/contexts/DocumentContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const DocumentAnnotationsContext = React.createContext();
const DocumentContext = React.createContext();
const DocumentFiltersContext = React.createContext();
const DocumentActiveAnnotationsContext = React.createContext();
const WebsocketContext = React.createContext();

export {
DocumentAnnotationsContext,
DocumentContext,
DocumentFiltersContext,
DocumentActiveAnnotationsContext,
WebsocketContext,
};
46 changes: 44 additions & 2 deletions src/pages/_app.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable react/jsx-props-no-spreading */
import { useState } from 'react';
import { useState, useCallback, useEffect } from 'react';
import useWebSocket, { ReadyState } from 'react-use-websocket';
import { SessionProvider } from 'next-auth/react';
import {
WebsocketContext
} from '../contexts/DocumentContext';
import Router from 'next/router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
Expand All @@ -17,9 +21,47 @@ Router.events.on('routeChangeError', () => NProgress.done());

export default function AnnotationStudio({ Component, pageProps }) {
const [session, setSession] = useState(pageProps.session);

const [socketUrl, setSocketUrl] = useState('wss://wq5pn518mf.execute-api.us-east-2.amazonaws.com/dev/');
const [messageHistory, setMessageHistory] = useState([]);

const {
sendMessage,
sendJsonMessage,
lastMessage,
lastJsonMessage,
readyState,
getWebSocket,
} = useWebSocket(socketUrl, {
onOpen: () => console.log('opened'),
//Will attempt to reconnect on all close events, such as server shutting down
shouldReconnect: (closeEvent) => true,
});

const connectionStatus = {
[ReadyState.CONNECTING]: 'Connecting',
[ReadyState.OPEN]: 'Open',
[ReadyState.CLOSING]: 'Closing',
[ReadyState.CLOSED]: 'Closed',
[ReadyState.UNINSTANTIATED]: 'Uninstantiated',
}[readyState];


const handleSendJsonMessage = useCallback((json) => sendJsonMessage(json), []);


useEffect(() => {
if (lastJsonMessage !== null) {
// console.log('lastJsonMessage: ', lastJsonMessage)
setMessageHistory((prev) => prev.concat(lastJsonMessage));
}
}, [lastJsonMessage, setMessageHistory]);

return (
<SessionProvider session={pageProps.session}>
<Component {...pageProps} statefulSession={session} updateSession={setSession} />
<WebsocketContext.Provider value={[messageHistory, setMessageHistory, handleSendJsonMessage, lastJsonMessage, readyState, connectionStatus, getWebSocket]}>
<Component {...pageProps} statefulSession={session} updateSession={setSession} />
</WebsocketContext.Provider>
</SessionProvider>
);
}
2 changes: 1 addition & 1 deletion src/pages/api/document/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const handler = async (req, res) => {
.findOne({ _id: ObjectID(token.sub) });
const findCondition = {
$and: [
{ _id: ObjectID(req.query.id) }
{ _id: ObjectID(req.query.id) },
],
};
if (userObj.role !== 'admin') findCondition.$and.push({ owner: token.sub });
Expand Down
Loading

0 comments on commit e688c58

Please sign in to comment.