Skip to content

Commit

Permalink
refactor(socket.io): Switch to V2 + Vite (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
machour authored May 29, 2024
1 parent 35dfc53 commit ae70493
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 147 deletions.
84 changes: 84 additions & 0 deletions socket.io/.eslintrc.cjs
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,
},
},
],
};
4 changes: 0 additions & 4 deletions socket.io/.eslintrc.js

This file was deleted.

3 changes: 1 addition & 2 deletions socket.io/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules

/.cache
/server/build
/public/build
/build
.env
42 changes: 20 additions & 22 deletions socket.io/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { MetaFunction } from "@remix-run/node";
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
Expand All @@ -13,11 +11,23 @@ import io from "socket.io-client";

import { SocketProvider } from "~/context";

export const meta: MetaFunction = () => ({
charset: "utf-8",
title: "New Remix App",
viewport: "width=device-width,initial-scale=1",
});
export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}

export default function App() {
const [socket, setSocket] = useState<Socket>();
Expand All @@ -38,19 +48,7 @@ export default function App() {
}, [socket]);

return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<SocketProvider socket={socket}>
<Outlet />
</SocketProvider>
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
<SocketProvider socket={socket}>
<Outlet />
</SocketProvider>);
}
53 changes: 33 additions & 20 deletions socket.io/package.json
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"
}
}
}
11 changes: 0 additions & 11 deletions socket.io/remix.config.js

This file was deleted.

2 changes: 0 additions & 2 deletions socket.io/remix.env.d.ts

This file was deleted.

77 changes: 77 additions & 0 deletions socket.io/server.js
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}`)
});

Loading

0 comments on commit ae70493

Please sign in to comment.