- Created a basic Socket.IO server that manages user connections and queues for events. - Implemented queue logic to handle concurrent user limits and JWT token issuance. - Added MySQL configuration for potential persistence of queue positions. - Introduced environment variables for configuration through a .env.example file.
89 lines
3.5 KiB
Markdown
89 lines
3.5 KiB
Markdown
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
|
|
|
|
## Getting Started
|
|
|
|
First, run the development server:
|
|
|
|
```bash
|
|
npm run dev
|
|
# or
|
|
yarn dev
|
|
# or
|
|
pnpm dev
|
|
# or
|
|
bun dev
|
|
```
|
|
|
|
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
|
|
|
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
|
|
|
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
|
|
|
## Learn More
|
|
|
|
To learn more about Next.js, take a look at the following resources:
|
|
|
|
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
|
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
|
|
|
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
|
|
|
|
## Deploy on Vercel
|
|
|
|
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
|
|
|
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
|
|
|
## Queue / Ticketing demo (WebSocket + JWT)
|
|
|
|
This repository contains a minimal demo of a queue system for high-concurrency ticket sales. Key ideas:
|
|
|
|
- When an event reaches a concurrency threshold (e.g. 100 users), the server enables a queue for that event.
|
|
- Users connect via WebSocket (Socket.IO). When queueing is active they are placed in a FIFO queue.
|
|
- The server grants access (issues a signed JWT) to up to CONCURRENT_ACTIVE users (default 50) at a time.
|
|
- The JWT is valid for a short period (default 15 minutes). When it expires the server revokes access and grants the next users in queue.
|
|
- The client uses the JWT to authenticate purchase API calls.
|
|
|
|
Files added in this demo:
|
|
|
|
- `server/index.js` — simple Socket.IO server with in-memory queue logic and JWT issuance (demo only).
|
|
- `.env.example` — example environment variables for running the socket server.
|
|
- `app/page.tsx` — client UI (Next.js) that connects via Socket.IO and displays queue position, estimated wait, and purchase button.
|
|
|
|
Important notes and limitations:
|
|
|
|
- The server implementation in `server/index.js` is an in-memory demo. For production use a shared, persistent store (Redis) for queue/locks and a robust MySQL schema if you need persistence.
|
|
- Add authentication, rate-limiting, and persistent sessions before using in production.
|
|
|
|
Running locally
|
|
|
|
1. Copy `.env.example` to `.env` and adjust values (especially `JWT_SECRET`).
|
|
2. Install dependencies for the server and client:
|
|
|
|
```powershell
|
|
pnpm install
|
|
pnpm --filter . add socket.io socket.io-client jsonwebtoken dotenv mysql2
|
|
```
|
|
|
|
3. Start the Socket.IO server:
|
|
|
|
```powershell
|
|
node server/index.js
|
|
```
|
|
|
|
4. Start the Next.js dev server:
|
|
|
|
```powershell
|
|
pnpm dev
|
|
```
|
|
|
|
5. Open `http://localhost:3000` and the client will connect to the socket server (default `http://localhost:4000`).
|
|
|
|
Next steps / improvements
|
|
|
|
- Move queue state to Redis (ZSET) to support multiple server instances.
|
|
- Persist issued tokens and sessions in DB to support revocation and auditing.
|
|
- Add server-side validation of the JWT on purchase endpoints.
|
|
- Add tests around queue promotion logic and token expiration.
|