-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(socket.io): Switch to V2 + Vite (#490)
- Loading branch information
Showing
11 changed files
with
246 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* This is intended to be a basic starting point for linting in your app. | ||
* It relies on recommended configs out of the box for simplicity, but you can | ||
* and should modify this configuration to best suit your team's needs. | ||
*/ | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
module.exports = { | ||
root: true, | ||
parserOptions: { | ||
ecmaVersion: "latest", | ||
sourceType: "module", | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
env: { | ||
browser: true, | ||
commonjs: true, | ||
es6: true, | ||
}, | ||
ignorePatterns: ["!**/.server", "!**/.client"], | ||
|
||
// Base config | ||
extends: ["eslint:recommended"], | ||
|
||
overrides: [ | ||
// React | ||
{ | ||
files: ["**/*.{js,jsx,ts,tsx}"], | ||
plugins: ["react", "jsx-a11y"], | ||
extends: [ | ||
"plugin:react/recommended", | ||
"plugin:react/jsx-runtime", | ||
"plugin:react-hooks/recommended", | ||
"plugin:jsx-a11y/recommended", | ||
], | ||
settings: { | ||
react: { | ||
version: "detect", | ||
}, | ||
formComponents: ["Form"], | ||
linkComponents: [ | ||
{ name: "Link", linkAttribute: "to" }, | ||
{ name: "NavLink", linkAttribute: "to" }, | ||
], | ||
"import/resolver": { | ||
typescript: {}, | ||
}, | ||
}, | ||
}, | ||
|
||
// Typescript | ||
{ | ||
files: ["**/*.{ts,tsx}"], | ||
plugins: ["@typescript-eslint", "import"], | ||
parser: "@typescript-eslint/parser", | ||
settings: { | ||
"import/internal-regex": "^~/", | ||
"import/resolver": { | ||
node: { | ||
extensions: [".ts", ".tsx"], | ||
}, | ||
typescript: { | ||
alwaysTryTypes: true, | ||
}, | ||
}, | ||
}, | ||
extends: [ | ||
"plugin:@typescript-eslint/recommended", | ||
"plugin:import/recommended", | ||
"plugin:import/typescript", | ||
], | ||
}, | ||
|
||
// Node | ||
{ | ||
files: [".eslintrc.cjs", "server.js"], | ||
env: { | ||
node: true, | ||
}, | ||
}, | ||
], | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
node_modules | ||
|
||
/.cache | ||
/server/build | ||
/public/build | ||
/build | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,49 @@ | ||
{ | ||
"name": "eps", | ||
"private": true, | ||
"sideEffects": false, | ||
"type": "module", | ||
"scripts": { | ||
"build": "remix build", | ||
"dev": "remix watch", | ||
"start": "cross-env NODE_ENV=production node server/index.js", | ||
"start:dev": "cross-env NODE_ENV=development node server/index.js", | ||
"build": "remix vite:build", | ||
"dev": "node ./server.js", | ||
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .", | ||
"start": "cross-env NODE_ENV=production node ./server.js", | ||
"typecheck": "tsc" | ||
}, | ||
"dependencies": { | ||
"@remix-run/express": "^1.19.3", | ||
"@remix-run/node": "^1.19.3", | ||
"@remix-run/react": "^1.19.3", | ||
"@remix-run/express": "^2.9.2", | ||
"@remix-run/node": "^2.9.2", | ||
"@remix-run/react": "^2.9.2", | ||
"compression": "^1.7.4", | ||
"cross-env": "^7.0.3", | ||
"express": "^4.17.3", | ||
"express": "^4.18.2", | ||
"isbot": "^4.1.0", | ||
"morgan": "^1.10.0", | ||
"isbot": "^3.6.5", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"socket.io": "^4.4.1", | ||
"socket.io-client": "^4.4.1" | ||
"socket.io": "^4.7.5", | ||
"socket.io-client": "^4.7.5" | ||
}, | ||
"devDependencies": { | ||
"@remix-run/dev": "^1.19.3", | ||
"@remix-run/eslint-config": "^1.19.3", | ||
"@types/react": "^18.0.25", | ||
"@types/react-dom": "^18.0.8", | ||
"eslint": "^8.27.0", | ||
"typescript": "^4.8.4" | ||
"@remix-run/dev": "^2.9.2", | ||
"@types/compression": "^1.7.5", | ||
"@types/express": "^4.17.20", | ||
"@types/morgan": "^1.9.9", | ||
"@types/react": "^18.2.20", | ||
"@types/react-dom": "^18.2.7", | ||
"@typescript-eslint/eslint-plugin": "^6.7.4", | ||
"@typescript-eslint/parser": "^6.7.4", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.38.0", | ||
"eslint-import-resolver-typescript": "^3.6.1", | ||
"eslint-plugin-import": "^2.28.1", | ||
"eslint-plugin-jsx-a11y": "^6.7.1", | ||
"eslint-plugin-react": "^7.33.2", | ||
"eslint-plugin-react-hooks": "^4.6.0", | ||
"typescript": "^5.1.6", | ||
"vite": "^5.1.0", | ||
"vite-tsconfig-paths": "^4.2.1" | ||
}, | ||
"engines": { | ||
"node": ">=14.0.0" | ||
"node": ">=20.0.0" | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createRequestHandler } from "@remix-run/express"; | ||
import compression from "compression"; | ||
import express from "express"; | ||
import morgan from "morgan"; | ||
import { createServer } from "http"; | ||
import { Server } from "socket.io"; | ||
|
||
const viteDevServer = | ||
process.env.NODE_ENV === "production" | ||
? undefined | ||
: await import("vite").then((vite) => | ||
vite.createServer({ | ||
server: { middlewareMode: true }, | ||
}) | ||
); | ||
|
||
const remixHandler = createRequestHandler({ | ||
build: viteDevServer | ||
? () => viteDevServer.ssrLoadModule("virtual:remix/server-build") | ||
: await import("./build/server/index.js"), | ||
}); | ||
|
||
const app = express(); | ||
|
||
// You need to create the HTTP server from the Express app | ||
const httpServer = createServer(app); | ||
|
||
// And then attach the socket.io server to the HTTP server | ||
const io = new Server(httpServer); | ||
|
||
// Then you can use `io` to listen the `connection` event and get a socket | ||
// from a client | ||
io.on("connection", (socket) => { | ||
// from this point you are on the WS connection with a specific client | ||
console.log(socket.id, "connected"); | ||
|
||
socket.emit("confirmation", "connected!"); | ||
|
||
socket.on("event", (data) => { | ||
console.log(socket.id, data); | ||
socket.emit("event", "pong"); | ||
}); | ||
}); | ||
|
||
|
||
app.use(compression()); | ||
|
||
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header | ||
app.disable("x-powered-by"); | ||
|
||
// handle asset requests | ||
if (viteDevServer) { | ||
app.use(viteDevServer.middlewares); | ||
} else { | ||
// Vite fingerprints its assets so we can cache forever. | ||
app.use( | ||
"/assets", | ||
express.static("build/client/assets", { immutable: true, maxAge: "1y" }) | ||
); | ||
} | ||
|
||
// Everything else (like favicon.ico) is cached for an hour. You may want to be | ||
// more aggressive with this caching. | ||
app.use(express.static("build/client", { maxAge: "1h" })); | ||
|
||
app.use(morgan("tiny")); | ||
|
||
// handle SSR requests | ||
app.all("*", remixHandler); | ||
|
||
const port = process.env.PORT || 3000; | ||
|
||
// instead of running listen on the Express app, do it on the HTTP server | ||
httpServer.listen(port, () => { | ||
console.log(`Express server listening at http://localhost:${port}`) | ||
}); | ||
|
Oops, something went wrong.