From d744fda77207ecf1fa4bc1cd3d32eaa403deca9f Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 13 Sep 2023 15:52:56 +0200 Subject: [PATCH] docs: improve example with express-session The example is now available with different syntaxes: - CommonJS - ES modules - TypeScript Related: https://github.com/socketio/socket.io/pull/4787 --- .../{ => cjs}/index.html | 0 examples/express-session-example/cjs/index.js | 66 +++++++++++++++++ .../express-session-example/cjs/package.json | 15 ++++ .../express-session-example/esm/index.html | 61 ++++++++++++++++ .../{ => esm}/index.js | 4 +- .../{ => esm}/package.json | 0 .../express-session-example/ts/index.html | 61 ++++++++++++++++ examples/express-session-example/ts/index.ts | 72 +++++++++++++++++++ .../express-session-example/ts/package.json | 20 ++++++ .../express-session-example/ts/tsconfig.json | 11 +++ 10 files changed, 308 insertions(+), 2 deletions(-) rename examples/express-session-example/{ => cjs}/index.html (100%) create mode 100644 examples/express-session-example/cjs/index.js create mode 100644 examples/express-session-example/cjs/package.json create mode 100644 examples/express-session-example/esm/index.html rename examples/express-session-example/{ => esm}/index.js (92%) rename examples/express-session-example/{ => esm}/package.json (100%) create mode 100644 examples/express-session-example/ts/index.html create mode 100644 examples/express-session-example/ts/index.ts create mode 100644 examples/express-session-example/ts/package.json create mode 100644 examples/express-session-example/ts/tsconfig.json diff --git a/examples/express-session-example/index.html b/examples/express-session-example/cjs/index.html similarity index 100% rename from examples/express-session-example/index.html rename to examples/express-session-example/cjs/index.html diff --git a/examples/express-session-example/cjs/index.js b/examples/express-session-example/cjs/index.js new file mode 100644 index 0000000000..e3aaac83ee --- /dev/null +++ b/examples/express-session-example/cjs/index.js @@ -0,0 +1,66 @@ +const express = require("express"); +const { createServer } = require("node:http"); +const { join } = require("node:path"); +const { Server } = require("socket.io"); +const session = require("express-session"); + +const port = process.env.PORT || 3000; + +const app = express(); +const httpServer = createServer(app); + +const sessionMiddleware = session({ + secret: "changeit", + resave: true, + saveUninitialized: true, +}); + +app.use(sessionMiddleware); + +app.get("/", (req, res) => { + res.sendFile(join(__dirname, "index.html")); +}); + +app.post("/incr", (req, res) => { + const session = req.session; + session.count = (session.count || 0) + 1; + res.status(200).end("" + session.count); + + io.to(session.id).emit("current count", session.count); +}); + +app.post("/logout", (req, res) => { + const sessionId = req.session.id; + req.session.destroy(() => { + // disconnect all Socket.IO connections linked to this session ID + io.to(sessionId).disconnectSockets(); + res.status(204).end(); + }); +}); + +const io = new Server(httpServer); + +io.engine.use(sessionMiddleware); + +io.on("connection", (socket) => { + const req = socket.request; + + socket.join(req.session.id); + + socket.on("incr", (cb) => { + req.session.reload((err) => { + if (err) { + // session has expired + return socket.disconnect(); + } + req.session.count = (req.session.count || 0) + 1; + req.session.save(() => { + cb(req.session.count); + }); + }); + }); +}); + +httpServer.listen(port, () => { + console.log(`application is running at: http://localhost:${port}`); +}); diff --git a/examples/express-session-example/cjs/package.json b/examples/express-session-example/cjs/package.json new file mode 100644 index 0000000000..af1aeccf8b --- /dev/null +++ b/examples/express-session-example/cjs/package.json @@ -0,0 +1,15 @@ +{ + "name": "express-session-example", + "version": "0.0.1", + "private": true, + "type": "commonjs", + "description": "Example with express-session (https://github.com/expressjs/session)", + "scripts": { + "start": "node index.js" + }, + "dependencies": { + "express": "~4.17.3", + "express-session": "~1.17.2", + "socket.io": "^4.7.2" + } +} diff --git a/examples/express-session-example/esm/index.html b/examples/express-session-example/esm/index.html new file mode 100644 index 0000000000..890fca1c6c --- /dev/null +++ b/examples/express-session-example/esm/index.html @@ -0,0 +1,61 @@ + + + + + Example with express-session + + + + +

Count: 0

+ + +

Status: disconnected

+

Count: 0

+ + + + + diff --git a/examples/express-session-example/index.js b/examples/express-session-example/esm/index.js similarity index 92% rename from examples/express-session-example/index.js rename to examples/express-session-example/esm/index.js index e3eed9c23c..aec0d17d9c 100644 --- a/examples/express-session-example/index.js +++ b/examples/express-session-example/esm/index.js @@ -1,5 +1,5 @@ import express from "express"; -import { createServer } from "http"; +import { createServer } from "node:http"; import { Server } from "socket.io"; import session from "express-session"; @@ -17,7 +17,7 @@ const sessionMiddleware = session({ app.use(sessionMiddleware); app.get("/", (req, res) => { - res.sendFile("./index.html", { root: process.cwd() }); + res.sendFile(new URL("./index.html", import.meta.url).pathname); }); app.post("/incr", (req, res) => { diff --git a/examples/express-session-example/package.json b/examples/express-session-example/esm/package.json similarity index 100% rename from examples/express-session-example/package.json rename to examples/express-session-example/esm/package.json diff --git a/examples/express-session-example/ts/index.html b/examples/express-session-example/ts/index.html new file mode 100644 index 0000000000..890fca1c6c --- /dev/null +++ b/examples/express-session-example/ts/index.html @@ -0,0 +1,61 @@ + + + + + Example with express-session + + + + +

Count: 0

+ + +

Status: disconnected

+

Count: 0

+ + + + + diff --git a/examples/express-session-example/ts/index.ts b/examples/express-session-example/ts/index.ts new file mode 100644 index 0000000000..7e2a47ca7e --- /dev/null +++ b/examples/express-session-example/ts/index.ts @@ -0,0 +1,72 @@ +import express = require("express"); +import { createServer } from "http"; +import { Server } from "socket.io"; +import session from "express-session"; +import { type Request } from "express"; + +declare module "express-session" { + interface SessionData { + count: number; + } +} + +const port = process.env.PORT || 3000; + +const app = express(); +const httpServer = createServer(app); + +const sessionMiddleware = session({ + secret: "changeit", + resave: true, + saveUninitialized: true, +}); + +app.use(sessionMiddleware); + +app.get("/", (req, res) => { + res.sendFile(new URL("./index.html", import.meta.url).pathname); +}); + +app.post("/incr", (req, res) => { + const session = req.session; + session.count = (session.count || 0) + 1; + res.status(200).end("" + session.count); + + io.to(session.id).emit("current count", session.count); +}); + +app.post("/logout", (req, res) => { + const sessionId = req.session.id; + req.session.destroy(() => { + // disconnect all Socket.IO connections linked to this session ID + io.to(sessionId).disconnectSockets(); + res.status(204).end(); + }); +}); + +const io = new Server(httpServer); + +io.engine.use(sessionMiddleware); + +io.on("connection", (socket) => { + const req = socket.request as Request; + + socket.join(req.session.id); + + socket.on("incr", (cb) => { + req.session.reload((err) => { + if (err) { + // session has expired + return socket.disconnect(); + } + req.session.count = (req.session.count || 0) + 1; + req.session.save(() => { + cb(req.session.count); + }); + }); + }); +}); + +httpServer.listen(port, () => { + console.log(`application is running at: http://localhost:${port}`); +}); diff --git a/examples/express-session-example/ts/package.json b/examples/express-session-example/ts/package.json new file mode 100644 index 0000000000..48489fa51d --- /dev/null +++ b/examples/express-session-example/ts/package.json @@ -0,0 +1,20 @@ +{ + "name": "express-session-example", + "version": "0.0.1", + "private": true, + "type": "module", + "description": "Example with express-session (https://github.com/expressjs/session)", + "scripts": { + "start": "ts-node index.ts" + }, + "dependencies": { + "@types/express": "^4.17.17", + "@types/express-session": "^1.17.7", + "@types/node": "^20.6.0", + "express": "~4.17.3", + "express-session": "~1.17.2", + "socket.io": "^4.7.2", + "ts-node": "^10.9.1", + "typescript": "^5.2.2" + } +} diff --git a/examples/express-session-example/ts/tsconfig.json b/examples/express-session-example/ts/tsconfig.json new file mode 100644 index 0000000000..fe03ed87a1 --- /dev/null +++ b/examples/express-session-example/ts/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "moduleResolution": "NodeNext", + "target": "ES2022", + "strict": true + }, + "ts-node": { + "esm": true + } +}