├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── code-sync.png ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── server.js └── src ├── Actions.js ├── App.css ├── App.js ├── App.test.js ├── components ├── Client.js └── Editor.js ├── index.css ├── index.js ├── logo.svg ├── pages ├── EditorPage.js └── Home.js ├── reportWebVitals.js ├── setupTests.js └── socket.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | .env 12 | 13 | # production 14 | /build 15 | 16 | # misc 17 | .DS_Store 18 | .env.local 19 | .env.development.local 20 | .env.test.local 21 | .env.production.local 22 | 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Demo Video: 2 | 3 | 4 | https://user-images.githubusercontent.com/65965671/224958307-09ef011a-f028-4a45-b55c-41eb60616ff8.mp4 5 | 6 | 7 | # Getting Started with Create React App 8 | 9 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 10 | 11 | ## Available Scripts 12 | 13 | In the project directory, you can run: 14 | 15 | ### `npm run server:dev` 16 | 17 | Runs the app in the development mode.\ 18 | Open [http://localhost:5001] to view it in your browser. 19 | 20 | The page will reload when you make changes.\ 21 | You may also see any lint errors in the console. 22 | 23 | ### `npm test` 24 | 25 | Launches the test runner in the interactive watch mode.\ 26 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 27 | 28 | ### `npm run build` 29 | 30 | Builds the app for production to the `build` folder.\ 31 | It correctly bundles React in production mode and optimizes the build for the best performance. 32 | 33 | The build is minified and the filenames include the hashes.\ 34 | Your app is ready to be deployed! 35 | 36 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 37 | 38 | ### `npm run eject` 39 | 40 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 41 | 42 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 43 | 44 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. 45 | 46 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. 47 | 48 | ## Learn More 49 | 50 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 51 | 52 | To learn React, check out the [React documentation](https://reactjs.org/). 53 | 54 | ### Code Splitting 55 | 56 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 57 | 58 | ### Analyzing the Bundle Size 59 | 60 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 61 | 62 | ### Making a Progressive Web App 63 | 64 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 65 | 66 | ### Advanced Configuration 67 | 68 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 69 | 70 | ### Deployment 71 | 72 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 73 | 74 | ### `npm run build` fails to minify 75 | 76 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "realtime-editor", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.16.2", 7 | "@testing-library/react": "^12.1.3", 8 | "@testing-library/user-event": "^13.5.0", 9 | "axios": "^1.3.4", 10 | "codemirror": "^5.48.4", 11 | "express": "^4.17.3", 12 | "react": "^17.0.2", 13 | "react-avatar": "^4.0.0", 14 | "react-dom": "^17.0.2", 15 | "react-hot-toast": "^2.2.0", 16 | "react-router-dom": "^6.2.1", 17 | "react-scripts": "5.0.0", 18 | "socket.io": "^4.4.1", 19 | "socket.io-client": "^4.4.1", 20 | "uuid": "^8.3.2", 21 | "web-vitals": "^2.1.4" 22 | }, 23 | "scripts": { 24 | "start:front": "react-scripts start", 25 | "start": "npm run server:prod", 26 | "build": "react-scripts build", 27 | "server:dev": "nodemon server.js", 28 | "server:prod": "node server.js", 29 | "test": "react-scripts test", 30 | "eject": "react-scripts eject" 31 | }, 32 | "eslintConfig": { 33 | "extends": [ 34 | "react-app", 35 | "react-app/jest" 36 | ] 37 | }, 38 | "browserslist": { 39 | "production": [ 40 | ">0.2%", 41 | "not dead", 42 | "not op_mini all" 43 | ], 44 | "development": [ 45 | "last 1 chrome version", 46 | "last 1 firefox version", 47 | "last 1 safari version" 48 | ] 49 | }, 50 | "devDependencies": { 51 | "nodemon": "^2.0.20" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /public/code-sync.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rupak5/realtime-code-editor/b88ef7bce984deb8f87e4761bc0775913d986840/public/code-sync.png -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rupak5/realtime-code-editor/b88ef7bce984deb8f87e4761bc0775913d986840/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | Code Sync 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rupak5/realtime-code-editor/b88ef7bce984deb8f87e4761bc0775913d986840/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rupak5/realtime-code-editor/b88ef7bce984deb8f87e4761bc0775913d986840/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const app = express(); 3 | const http = require("http"); 4 | const path = require("path"); 5 | const { Server } = require("socket.io"); 6 | const ACTIONS = require("./src/Actions"); 7 | 8 | const server = http.createServer(app); 9 | const io = new Server(server); 10 | 11 | app.use(express.static("build")); 12 | app.use((req, res, next) => { 13 | res.sendFile(path.join(__dirname, "build", "index.html")); 14 | }); 15 | 16 | const userSocketMap = {}; 17 | 18 | function getAllConnectedClients(roomId) { 19 | // Map 20 | return Array.from(io.sockets.adapter.rooms.get(roomId) || []).map( 21 | (socketId) => { 22 | return { 23 | socketId, 24 | username: userSocketMap[socketId], 25 | }; 26 | } 27 | ); 28 | } 29 | 30 | io.on("connection", (socket) => { 31 | console.log("socket connected", socket.id); 32 | socket.on(ACTIONS.JOIN, ({ roomId, username }) => { 33 | userSocketMap[socket.id] = username; 34 | socket.join(roomId); 35 | const clients = getAllConnectedClients(roomId); 36 | clients.forEach(({ socketId }) => { 37 | io.to(socketId).emit(ACTIONS.JOINED, { 38 | clients, 39 | username, 40 | socketId: socket.id, 41 | }); 42 | }); 43 | }); 44 | 45 | socket.on(ACTIONS.CODE_CHANGE, ({ roomId, code }) => { 46 | socket.in(roomId).emit(ACTIONS.CODE_CHANGE, { code }); 47 | }); 48 | 49 | socket.on(ACTIONS.SEND_MESSAGE, ({ roomId, message }) => { 50 | socket.in(roomId).emit(ACTIONS.SEND_MESSAGE, { message }); 51 | }); 52 | 53 | socket.on(ACTIONS.SYNC_CODE, ({ socketId, code }) => { 54 | io.to(socketId).emit(ACTIONS.CODE_CHANGE, { code }); 55 | }); 56 | 57 | socket.on("disconnecting", () => { 58 | const rooms = [...socket.rooms]; 59 | rooms.forEach((roomId) => { 60 | socket.in(roomId).emit(ACTIONS.DISCONNECTED, { 61 | socketId: socket.id, 62 | username: userSocketMap[socket.id], 63 | }); 64 | }); 65 | delete userSocketMap[socket.id]; 66 | socket.leave(); 67 | }); 68 | }); 69 | 70 | const PORT = process.env.PORT || 5001; 71 | 72 | server.listen(PORT, () => console.log(`Listening on port ${PORT}`)); 73 | -------------------------------------------------------------------------------- /src/Actions.js: -------------------------------------------------------------------------------- 1 | const ACTIONS = { 2 | JOIN: "join", 3 | JOINED: "joined", 4 | DISCONNECTED: "disconnected", 5 | CODE_CHANGE: "code-change", 6 | SYNC_CODE: "sync-code", 7 | LEAVE: "leave", 8 | SEND_MESSAGE: "send-message", 9 | }; 10 | 11 | module.exports = ACTIONS; 12 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .mainWrap { 2 | display: flex; 3 | height: 100%; 4 | width: 100%; 5 | flex-direction: row; 6 | } 7 | 8 | .asideWrap { 9 | display: flex; 10 | background: #1c1e29; 11 | padding: 10px; 12 | color: #fff; 13 | flex-direction: column; 14 | width: 13%; 15 | } 16 | 17 | .editorWrap { 18 | display: grid; 19 | padding: 10px; 20 | width: 60%; 21 | height: 100%; 22 | } 23 | 24 | .chatWrap { 25 | display: flex; 26 | flex-direction: column; 27 | width: 25%; 28 | height: 100%; 29 | } 30 | 31 | .asideInner { 32 | flex: 1; 33 | } 34 | 35 | .logo { 36 | border-bottom: 1px solid #424242; 37 | padding-bottom: 1px; 38 | } 39 | 40 | .logoImage { 41 | height: 60px; 42 | } 43 | 44 | .homePageWrapper { 45 | display: flex; 46 | align-items: center; 47 | justify-content: center; 48 | color: #fff; 49 | height: 100vh; 50 | } 51 | 52 | .formWrapper { 53 | background: #282a36; 54 | padding: 20px; 55 | border-radius: 10px; 56 | width: 400px; 57 | max-width: 90%; 58 | } 59 | 60 | footer { 61 | position: fixed; 62 | bottom: 0; 63 | } 64 | 65 | footer a { 66 | color: #4aee88; 67 | } 68 | 69 | .inputGroup { 70 | display: flex; 71 | flex-direction: column; 72 | } 73 | 74 | .mainLabel { 75 | margin-bottom: 20px; 76 | margin-top: 0; 77 | } 78 | 79 | .homePageLogo { 80 | height: 80px; 81 | margin-bottom: 30px; 82 | } 83 | 84 | .copyBtn { 85 | background: white; 86 | } 87 | 88 | .btn { 89 | border: none; 90 | padding: 10px; 91 | border-radius: 5px; 92 | font-size: 16px; 93 | font-weight: bold; 94 | cursor: pointer; 95 | transition: all 0.3s ease-in-out; 96 | } 97 | 98 | .joinBtn { 99 | background: #4aed88; 100 | width: 100px; 101 | margin-left: auto; 102 | } 103 | 104 | .leaveBtn { 105 | background: #4aed88; 106 | width: 100%; 107 | margin-left: auto; 108 | } 109 | 110 | .joinBtn:hover, 111 | .leaveBtn:hover { 112 | background: #2b824c; 113 | } 114 | 115 | .createInfo { 116 | margin: 0 auto; 117 | margin-top: 20px; 118 | } 119 | 120 | .createNewBtn { 121 | color: #4aed88; 122 | text-decoration: none; 123 | border-bottom: 1px solid #4aed88; 124 | transition: all 0.3s ease-in-out; 125 | } 126 | 127 | .createNewBtn:hover, 128 | footer a:hover { 129 | color: #368654; 130 | border-color: #368654; 131 | } 132 | 133 | .clientsList { 134 | display: flex; 135 | align-items: center; 136 | flex-wrap: wrap; 137 | gap: 20px; 138 | } 139 | 140 | .client { 141 | display: flex; 142 | align-items: center; 143 | flex-direction: column; 144 | font-weight: bold; 145 | } 146 | 147 | .userName { 148 | margin-top: 10px; 149 | } 150 | 151 | .seLang { 152 | border: none; 153 | width: 100%; 154 | margin-top: 10px; 155 | padding: 10px; 156 | border-radius: 5px; 157 | font-size: 16px; 158 | font-weight: bold; 159 | text-align-last: center; 160 | } 161 | 162 | .copyBtn, 163 | .runBtn, 164 | .leaveBtn { 165 | width: 100%; 166 | margin-top: 20px; 167 | } 168 | 169 | .CodeMirror { 170 | font-size: 18px; 171 | line-height: 1.5; 172 | min-height: calc(72vh); 173 | width: 100%; 174 | } 175 | 176 | .IO-container { 177 | display: flex; 178 | justify-content: left; 179 | min-height: calc(4vh); 180 | width: 100%; 181 | } 182 | 183 | .inputArea { 184 | font-size: 16px; 185 | line-height: 1.6; 186 | padding-top: 10px; 187 | background-color: rgba(0, 0, 0, 0.401); 188 | color: white; 189 | font-size: 16px; 190 | height: calc(15vh); 191 | width: 100%; 192 | } 193 | 194 | .chatArea { 195 | font-size: 18px; 196 | line-height: 1.6; 197 | padding-top: 10px; 198 | background-color: #1c1e29; 199 | color: white; 200 | font-size: 16px; 201 | border: 1px solid transparent; 202 | min-height: calc(85vh); 203 | height: calc(92vh); 204 | } 205 | 206 | .sendChatWrap { 207 | display: flex; 208 | height: calc(5vh); 209 | } 210 | 211 | .sendBtn { 212 | width: 20%; 213 | margin-right: 5px; 214 | } 215 | 216 | .inputBox { 217 | padding: 10px; 218 | border-radius: 5px; 219 | outline: none; 220 | border: none; 221 | margin-bottom: 14px; 222 | background: #eee; 223 | font-size: 16px; 224 | font-weight: bold; 225 | } 226 | 227 | .sendBtn, 228 | .runBtn { 229 | color: white; 230 | background-color: rgb(99, 92, 232); 231 | } 232 | 233 | .clickedLabel { 234 | background-color: rgb(99, 92, 232); 235 | border: 1px solid transparent; 236 | color: white; 237 | padding: 10px 10px; 238 | text-align: center; 239 | text-decoration: none; 240 | display: inline-block; 241 | font-size: 16px; 242 | margin: 2px 2px; 243 | cursor: pointer; 244 | width: 10%; 245 | border-radius: 5px; 246 | font-weight: bold; 247 | cursor: pointer; 248 | transition: all 0.3s ease-in-out; 249 | } 250 | 251 | .notClickedLabel { 252 | background-color: rgb(189, 187, 225); 253 | border: 1px solid transparent; 254 | color: white; 255 | padding: 10px 10px; 256 | text-align: center; 257 | text-decoration: none; 258 | display: inline-block; 259 | font-size: 16px; 260 | margin: 2px 2px; 261 | cursor: pointer; 262 | width: 10%; 263 | border-radius: 5px; 264 | font-weight: bold; 265 | cursor: pointer; 266 | transition: all 0.3s ease-in-out; 267 | } 268 | 269 | .inputField { 270 | background-color: rgb(255, 255, 255); 271 | border: 1px solid transparent; 272 | color: rgb(0, 0, 0); 273 | padding: 5px 5px; 274 | text-align: left; 275 | text-decoration: none; 276 | display: inline-block; 277 | font-size: 16px; 278 | margin: 1px 1px; 279 | margin-left: 5px; 280 | margin-right: 5px; 281 | width: 70%; 282 | } 283 | 284 | .textarea-style::-webkit-scrollbar { 285 | width: 10px; 286 | } 287 | 288 | .textarea-style::-webkit-scrollbar-track { 289 | background: #1c1e29; 290 | } 291 | 292 | .textarea-style::-webkit-scrollbar-thumb { 293 | background: rgb(99, 92, 232); 294 | } 295 | 296 | .textarea-style::-webkit-scrollbar-thumb:hover { 297 | background: rgb(189, 187, 225); 298 | } 299 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import "./App.css"; 2 | import { BrowserRouter, Routes, Route } from "react-router-dom"; 3 | import Home from "./pages/Home"; 4 | import EditorPage from "./pages/EditorPage"; 5 | import { Toaster } from "react-hot-toast"; 6 | 7 | function App() { 8 | return ( 9 | <> 10 |
11 | 21 |
22 | 23 | 24 | }> 25 | }> 26 | 27 | 28 | 29 | ); 30 | } 31 | 32 | export default App; 33 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/components/Client.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Avatar from "react-avatar"; 3 | 4 | const Client = ({ username }) => { 5 | return ( 6 |
7 | 8 | {username} 9 |
10 | ); 11 | }; 12 | 13 | export default Client; 14 | -------------------------------------------------------------------------------- /src/components/Editor.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useRef } from "react"; 2 | import CodeMirror from "codemirror"; 3 | import "codemirror/lib/codemirror.css"; 4 | import "codemirror/theme/dracula.css"; 5 | import "codemirror/mode/javascript/javascript"; 6 | import "codemirror/addon/edit/closetag"; 7 | import "codemirror/addon/edit/closebrackets"; 8 | import ACTIONS from "../Actions"; 9 | 10 | const Editor = ({ socketRef, roomId, onCodeChange }) => { 11 | const editorRef = useRef(null); 12 | 13 | useEffect(() => { 14 | async function init() { 15 | editorRef.current = CodeMirror.fromTextArea( 16 | document.getElementById("realtimeEditor"), 17 | { 18 | mode: { name: "javascript", json: true }, 19 | theme: "dracula", 20 | autoCloseTags: true, 21 | autoCloseBrackets: true, 22 | lineNumbers: true, 23 | } 24 | ); 25 | 26 | editorRef.current.on("change", (instance, changes) => { 27 | const { origin } = changes; 28 | const code = instance.getValue(); 29 | onCodeChange(code); 30 | if (origin !== "setValue") { 31 | socketRef.current.emit(ACTIONS.CODE_CHANGE, { 32 | roomId, 33 | code, 34 | }); 35 | } 36 | }); 37 | } 38 | init(); 39 | }, []); 40 | 41 | useEffect(() => { 42 | if (socketRef.current) { 43 | socketRef.current.on(ACTIONS.CODE_CHANGE, ({ code }) => { 44 | if (code != null) { 45 | editorRef.current.setValue(code); 46 | } 47 | }); 48 | } 49 | return () => { 50 | socketRef.current.off(ACTIONS.CODE_CHANGE); 51 | }; 52 | }, [socketRef.current]); 53 | 54 | return ; 55 | }; 56 | 57 | export default Editor; 58 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | background: #1c1e29; 9 | } 10 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | import reportWebVitals from "./reportWebVitals"; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById("root") 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/pages/EditorPage.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useRef, useEffect } from "react"; 2 | import ACTIONS from "../Actions"; 3 | import Client from "../components/Client"; 4 | import Editor from "../components/Editor"; 5 | import { initSocket } from "../socket"; 6 | import { 7 | Navigate, 8 | useLocation, 9 | useNavigate, 10 | useParams, 11 | } from "react-router-dom"; 12 | import { toast } from "react-hot-toast"; 13 | import axios from "axios"; 14 | 15 | const EditorPage = () => { 16 | const socketRef = useRef(null); 17 | const codeRef = useRef(null); 18 | const location = useLocation(); 19 | const reactNavigator = useNavigate(); 20 | const { roomId } = useParams(); 21 | const [clients, setClients] = useState([]); 22 | 23 | useEffect(() => { 24 | const init = async () => { 25 | socketRef.current = await initSocket(); 26 | 27 | socketRef.current.on("connect_error", (err) => handleErrors(err)); 28 | socketRef.current.on("connect_failed", (err) => handleErrors(err)); 29 | 30 | function handleErrors(e) { 31 | console.log("socket error", e); 32 | toast.error("Socket connection failed, try again later."); 33 | reactNavigator("/"); 34 | } 35 | 36 | socketRef.current.emit(ACTIONS.JOIN, { 37 | roomId, 38 | username: location.state?.username, 39 | }); 40 | 41 | //Listening for joined event 42 | socketRef.current.on( 43 | ACTIONS.JOINED, 44 | ({ clients, username, socketId }) => { 45 | if (username !== location.state?.username) { 46 | toast.success(`${username} joined the room.`); 47 | console.log(`${username} joined`); 48 | } 49 | setClients(clients); 50 | socketRef.current.emit(ACTIONS.SYNC_CODE, { 51 | code: codeRef.current, 52 | socketId, 53 | }); 54 | } 55 | ); 56 | 57 | //Listeing for disconnected 58 | socketRef.current.on(ACTIONS.DISCONNECTED, ({ socketId, username }) => { 59 | toast.success(`${username} left the room.`); 60 | setClients((prev) => { 61 | return prev.filter((client) => client.socketId !== socketId); 62 | }); 63 | }); 64 | 65 | //Listening for message 66 | socketRef.current.on(ACTIONS.SEND_MESSAGE, ({ message }) => { 67 | const chatWindow = document.getElementById("chatWindow"); 68 | var currText = chatWindow.value; 69 | currText += message; 70 | chatWindow.value = currText; 71 | chatWindow.scrollTop = chatWindow.scrollHeight; 72 | }); 73 | }; 74 | init(); 75 | return () => { 76 | socketRef.current.off(ACTIONS.JOINED); 77 | socketRef.current.off(ACTIONS.DISCONNECTED); 78 | socketRef.current.off(ACTIONS.SEND_MESSAGE); 79 | socketRef.current.disconnect(); 80 | }; 81 | }, []); 82 | 83 | async function copyRoomId() { 84 | try { 85 | await navigator.clipboard.writeText(roomId); 86 | toast.success("Room ID has been copied to your clipboard"); 87 | } catch (err) { 88 | toast.error("Could not copy the room id"); 89 | console.error(err); 90 | } 91 | } 92 | 93 | function leaveRoom() { 94 | reactNavigator("/"); 95 | } 96 | 97 | if (!location.state) { 98 | return ; 99 | } 100 | 101 | const inputClicked = () => { 102 | const inputArea = document.getElementById("input"); 103 | inputArea.placeholder = "Enter your input here"; 104 | inputArea.value = ""; 105 | inputArea.disabled = false; 106 | const inputLabel = document.getElementById("inputLabel"); 107 | const outputLabel = document.getElementById("outputLabel"); 108 | inputLabel.classList.remove("notClickedLabel"); 109 | inputLabel.classList.add("clickedLabel"); 110 | outputLabel.classList.remove("clickedLabel"); 111 | outputLabel.classList.add("notClickedLabel"); 112 | }; 113 | 114 | const outputClicked = () => { 115 | const inputArea = document.getElementById("input"); 116 | inputArea.placeholder = 117 | "You output will apear here, Click 'Run code' to see it"; 118 | inputArea.value = ""; 119 | inputArea.disabled = true; 120 | const inputLabel = document.getElementById("inputLabel"); 121 | const outputLabel = document.getElementById("outputLabel"); 122 | inputLabel.classList.remove("clickedLabel"); 123 | inputLabel.classList.add("notClickedLabel"); 124 | outputLabel.classList.remove("notClickedLabel"); 125 | outputLabel.classList.add("clickedLabel"); 126 | }; 127 | 128 | const runCode = () => { 129 | const lang = document.getElementById("languageOptions").value; 130 | const input = document.getElementById("input").value; 131 | const code = codeRef.current; 132 | 133 | toast.loading("Running Code...."); 134 | 135 | const encodedParams = new URLSearchParams(); 136 | encodedParams.append("LanguageChoice", lang); 137 | encodedParams.append("Program", code); 138 | encodedParams.append("Input", input); 139 | 140 | const options = { 141 | method: "POST", 142 | url: "https://code-compiler.p.rapidapi.com/v2", 143 | headers: { 144 | "content-type": "application/x-www-form-urlencoded", 145 | "X-RapidAPI-Key": process.env.REACT_APP_API_KEY, 146 | "X-RapidAPI-Host": "code-compiler.p.rapidapi.com", 147 | }, 148 | data: encodedParams, 149 | }; 150 | 151 | console.log(options); 152 | 153 | axios 154 | .request(options) 155 | .then(function (response) { 156 | let message = response.data.Result; 157 | if (message === null) { 158 | message = response.data.Errors; 159 | } 160 | outputClicked(); 161 | document.getElementById("input").value = message; 162 | toast.dismiss(); 163 | toast.success("Code compilation complete"); 164 | }) 165 | .catch(function (error) { 166 | toast.dismiss(); 167 | toast.error("Code compilation unsuccessful"); 168 | document.getElementById("input").value = 169 | "Something went wrong, Please check your code and input."; 170 | }); 171 | }; 172 | 173 | const sendMessage = () => { 174 | if (document.getElementById("inputBox").value === "") return; 175 | var message = `> ${location.state.username}:\n${ 176 | document.getElementById("inputBox").value 177 | }\n`; 178 | const chatWindow = document.getElementById("chatWindow"); 179 | var currText = chatWindow.value; 180 | currText += message; 181 | chatWindow.value = currText; 182 | chatWindow.scrollTop = chatWindow.scrollHeight; 183 | document.getElementById("inputBox").value = ""; 184 | socketRef.current.emit(ACTIONS.SEND_MESSAGE, { roomId, message }); 185 | }; 186 | 187 | const handleInputEnter = (key) => { 188 | if (key.code === "Enter") { 189 | sendMessage(); 190 | } 191 | }; 192 | 193 | return ( 194 |
195 |
196 |
197 |
198 | logo 199 |
200 |

Connected

201 |
202 | {clients.map((client) => ( 203 | 204 | ))} 205 |
206 |
207 | 228 | 231 | 234 | 237 |
238 | 239 |
240 | { 244 | codeRef.current = code; 245 | }} 246 | /> 247 |
248 | 255 | 262 |
263 | 268 |
269 | 270 |
271 | 277 |
278 | 285 | 288 |
289 |
290 |
291 | ); 292 | }; 293 | 294 | export default EditorPage; 295 | -------------------------------------------------------------------------------- /src/pages/Home.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable jsx-a11y/anchor-is-valid */ 2 | import React, { useState } from "react"; 3 | import { v4 as uuidV4 } from "uuid"; 4 | import toast from "react-hot-toast"; 5 | import { useNavigate } from "react-router-dom"; 6 | 7 | const Home = () => { 8 | const navigate = useNavigate(); 9 | const [roomId, setRoomId] = useState(""); 10 | const [username, setUsername] = useState(""); 11 | 12 | const createNewRoom = (e) => { 13 | e.preventDefault(); 14 | const id = uuidV4(); 15 | setRoomId(id); 16 | toast.success("Created a new room"); 17 | }; 18 | 19 | const joinRoom = () => { 20 | if (!username || !roomId) { 21 | toast.error("ROOM ID & username are required"); 22 | return; 23 | } 24 | //Redirect 25 | navigate(`/editor/${roomId}`, { 26 | state: { 27 | username, 28 | }, 29 | }); 30 | }; 31 | 32 | const handleInputEnter = (e) => { 33 | if (e.code === "Enter") { 34 | joinRoom(); 35 | } 36 | }; 37 | 38 | return ( 39 |
40 |
41 | code-sync-logo 46 |

Paste invitation ROOM ID

47 |
48 | setRoomId(e.target.value)} 53 | value={roomId} 54 | onKeyUp={handleInputEnter} 55 | /> 56 | setUsername(e.target.value)} 61 | value={username} 62 | onKeyUp={handleInputEnter} 63 | /> 64 | 67 | 68 | If you don't have an invite then create   69 | 70 | new room 71 | 72 | 73 |
74 |
75 | 78 |
79 | ); 80 | }; 81 | 82 | export default Home; 83 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /src/socket.js: -------------------------------------------------------------------------------- 1 | import { io } from "socket.io-client"; 2 | 3 | export const initSocket = async () => { 4 | const options = { 5 | "force new connection": true, 6 | reconnectionAttempt: "Infinity", 7 | timeout: 10000, 8 | transports: ["websocket"], 9 | }; 10 | return io(process.env.REACT_APP_BACKEND_URL, options); 11 | }; 12 | --------------------------------------------------------------------------------