ai-tutorial.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // AI Assistant Tutorial Response
  2. export const AI_TUTORIAL_MD = String.raw`
  3. # Building a Modern Chat Application with SvelteKit
  4. I'll help you create a **production-ready chat application** using SvelteKit, TypeScript, and WebSockets. This implementation includes real-time messaging, user authentication, and message persistence.
  5. ## 🚀 Quick Start
  6. First, let's set up the project:
  7. ${'```'}bash
  8. npm create svelte@latest chat-app
  9. cd chat-app
  10. npm install
  11. npm install socket.io socket.io-client
  12. npm install @prisma/client prisma
  13. npm run dev
  14. ${'```'}
  15. ## 📁 Project Structure
  16. ${'```'}
  17. chat-app/
  18. ├── src/
  19. │ ├── routes/
  20. │ │ ├── +layout.svelte
  21. │ │ ├── +page.svelte
  22. │ │ └── api/
  23. │ │ └── socket/+server.ts
  24. │ ├── lib/
  25. │ │ ├── components/
  26. │ │ │ ├── ChatMessage.svelte
  27. │ │ │ └── ChatInput.svelte
  28. │ │ └── stores/
  29. │ │ └── chat.ts
  30. │ └── app.html
  31. ├── prisma/
  32. │ └── schema.prisma
  33. └── package.json
  34. ${'```'}
  35. ## 💻 Implementation
  36. ### WebSocket Server
  37. ${'```'}typescript
  38. // src/lib/server/socket.ts
  39. import { Server } from 'socket.io';
  40. import type { ViteDevServer } from 'vite';
  41. export function initializeSocketIO(server: ViteDevServer) {
  42. const io = new Server(server.httpServer || server, {
  43. cors: {
  44. origin: process.env.ORIGIN || 'http://localhost:5173',
  45. credentials: true
  46. }
  47. });
  48. io.on('connection', (socket) => {
  49. console.log('User connected:', socket.id);
  50. socket.on('message', async (data) => {
  51. // Broadcast to all clients
  52. io.emit('new-message', {
  53. id: crypto.randomUUID(),
  54. userId: socket.id,
  55. content: data.content,
  56. timestamp: new Date().toISOString()
  57. });
  58. });
  59. socket.on('disconnect', () => {
  60. console.log('User disconnected:', socket.id);
  61. });
  62. });
  63. return io;
  64. }
  65. ${'```'}
  66. ### Client Store
  67. ${'```'}typescript
  68. // src/lib/stores/chat.ts
  69. import { writable } from 'svelte/store';
  70. import io from 'socket.io-client';
  71. export interface Message {
  72. id: string;
  73. userId: string;
  74. content: string;
  75. timestamp: string;
  76. }
  77. function createChatStore() {
  78. const { subscribe, update } = writable<Message[]>([]);
  79. let socket: ReturnType<typeof io>;
  80. return {
  81. subscribe,
  82. connect: () => {
  83. socket = io('http://localhost:5173');
  84. socket.on('new-message', (message: Message) => {
  85. update(messages => [...messages, message]);
  86. });
  87. },
  88. sendMessage: (content: string) => {
  89. if (socket && content.trim()) {
  90. socket.emit('message', { content });
  91. }
  92. }
  93. };
  94. }
  95. export const chatStore = createChatStore();
  96. ${'```'}
  97. ## 🎯 Key Features
  98. ✅ **Real-time messaging** with WebSockets
  99. ✅ **Message persistence** using Prisma + PostgreSQL
  100. ✅ **Type-safe** with TypeScript
  101. ✅ **Responsive UI** for all devices
  102. ✅ **Auto-reconnection** on connection loss
  103. ## 📊 Performance Metrics
  104. | Metric | Value |
  105. |--------|-------|
  106. | **Message Latency** | < 50ms |
  107. | **Concurrent Users** | 10,000+ |
  108. | **Messages/Second** | 5,000+ |
  109. | **Uptime** | 99.9% |
  110. ## 🔧 Configuration
  111. ### Environment Variables
  112. ${'```'}env
  113. DATABASE_URL="postgresql://user:password@localhost:5432/chat"
  114. JWT_SECRET="your-secret-key"
  115. REDIS_URL="redis://localhost:6379"
  116. ${'```'}
  117. ## 🚢 Deployment
  118. Deploy to production using Docker:
  119. ${'```'}dockerfile
  120. FROM node:20-alpine
  121. WORKDIR /app
  122. COPY package*.json ./
  123. RUN npm ci --only=production
  124. COPY . .
  125. RUN npm run build
  126. EXPOSE 3000
  127. CMD ["node", "build"]
  128. ${'```'}
  129. ---
  130. *Need help? Check the [documentation](https://kit.svelte.dev) or [open an issue](https://github.com/sveltejs/kit/issues)*
  131. `;