Skip to content

Commit 0d18c79

Browse files
committed
add rakkas example
1 parent d816353 commit 0d18c79

12 files changed

+2147
-538
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
strict-peer-dependencies = false
2+
shamefully-hoist=true

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
React hooks for socket.io 4.x
2+
3+
Examples:
4+
- [Next.js](examples/next/)
5+
- [Rakkas](examples/rakkas/)
6+
27
---
38
Usage: <br>
49
1. Wrap your components with the provider

examples/rakkas/.eslintrc.cjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require("@rakkasjs/eslint-config/patch");
2+
3+
module.exports = {
4+
root: true,
5+
extends: ["@rakkasjs"],
6+
parserOptions: { tsconfigRootDir: __dirname },
7+
settings: {
8+
"import/resolver": {
9+
typescript: {
10+
project: [__dirname + "/tsconfig.json"],
11+
},
12+
},
13+
},
14+
};

examples/rakkas/.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

examples/rakkas/global.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { IncomingMessage, ServerResponse, Server } from "http";
2+
import type { Server as SocketIOServer } from "socket.io";
3+
declare module "rakkasjs" {
4+
interface ServerSideLocals {
5+
io: SocketIOServer;
6+
}
7+
interface RequestContext {
8+
platform: {
9+
request: IncomingMessage & {
10+
socket: {
11+
server: Server & {
12+
io?: SocketIOServer;
13+
};
14+
};
15+
};
16+
response: ServerResponse;
17+
};
18+
}
19+
}

examples/rakkas/package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "example-rakkas",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"dev": "rakkas",
7+
"build": "rakkas build",
8+
"start": "node dist/server",
9+
"format": "prettier --write --ignore-unknown src",
10+
"test": "pnpm test:typecheck && pnpm test:format && pnpm test:lint",
11+
"test:typecheck": "tsc -p tsconfig.json --noEmit",
12+
"test:format": "prettier --check --ignore-unknown src",
13+
"test:lint": "eslint . --ignore-pattern dist"
14+
},
15+
"devDependencies": {
16+
"@rakkasjs/eslint-config": "0.6.19",
17+
"@types/react": "^18.0.28",
18+
"@types/react-dom": "^18.0.11",
19+
"eslint": "^8.34.0",
20+
"prettier": "^2.8.4",
21+
"rakkasjs": "0.6.19",
22+
"typescript": "^4.9.5",
23+
"vite": "^4.1.3",
24+
"vite-tsconfig-paths": "^4.0.5"
25+
},
26+
"dependencies": {
27+
"react": "^18.2.0",
28+
"react-dom": "^18.2.0",
29+
"socket.io": "^4.6.1",
30+
"socket.io-react-hook": "*"
31+
}
32+
}

examples/rakkas/src/common-hooks.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IoProvider } from "socket.io-react-hook";
2+
import type { CommonHooks } from "rakkasjs";
3+
4+
const hooks: CommonHooks = {
5+
wrapApp(app) {
6+
return <IoProvider>{app}</IoProvider>;
7+
},
8+
};
9+
10+
export default hooks;

examples/rakkas/src/entry-hattip.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createRequestHandler, RequestContext } from "rakkasjs";
2+
import { Server } from "socket.io";
3+
const socketIoMiddleware = (ctx: RequestContext) => {
4+
const server = ctx.platform.request.socket.server;
5+
if (!server.io) {
6+
server.io = new Server(server);
7+
}
8+
ctx.locals.io = server.io;
9+
};
10+
11+
export default createRequestHandler({
12+
middleware: {
13+
beforePages: [socketIoMiddleware],
14+
beforeApiRoutes: [socketIoMiddleware],
15+
},
16+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { runSSM } from "rakkasjs";
2+
import { useRef, useState } from "react";
3+
import { useSocketEvent } from "socket.io-react-hook";
4+
5+
export default function HomePage() {
6+
const ref = useRef<HTMLInputElement>(null);
7+
const [messages, setMessages] = useState<string[]>([]);
8+
9+
useSocketEvent<string>("message", {
10+
onMessage: (message) => setMessages((messages) => [...messages, message]),
11+
});
12+
13+
const sendMessage = (message: string) =>
14+
runSSM((ctx) => ctx.locals.io.emit("message", message));
15+
16+
return (
17+
<div>
18+
<button onClick={() => sendMessage(ref.current?.value ?? "")}>
19+
Send
20+
</button>
21+
<input type="text" ref={ref} />
22+
{messages.map((message, i) => (
23+
<div key={i}>{message}</div>
24+
))}
25+
</div>
26+
);
27+
}

examples/rakkas/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"module": "ESNext",
5+
"esModuleInterop": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"moduleResolution": "Node",
10+
"resolveJsonModule": true,
11+
"jsx": "react-jsx",
12+
"baseUrl": ".",
13+
"types": ["vite/client"]
14+
}
15+
}

examples/rakkas/vite.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vite";
2+
import rakkas from "rakkasjs/vite-plugin";
3+
import tsconfigPaths from "vite-tsconfig-paths";
4+
5+
export default defineConfig({
6+
plugins: [tsconfigPaths(), rakkas()],
7+
});

0 commit comments

Comments
 (0)