Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-instant-1· AnthropicHugging Face Arena Human Preference 55K

Create an server endpoint in sveltekit that streams JSON messages to a svelte component

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-3.5-turbo-0613’s answer.
  1. User

    Create an server endpoint in sveltekit that streams JSON messages to a svelte component

  2. Claude Assistant

    Here is an example of creating a server endpoint in SvelteKit that streams JSON messages to a Svelte component:

    server/index.js

    import { createServer } from 'http'
    import { serve } from 'sveltekit'
    
    // Generate some sample data
    const messages = [
      {id: 1, text: 'Hello'},
      {id: 2, text: 'World'}
    ]
    
    // Create HTTP server
    const server = createServer(async (req, res) => {
    
      // Handle requests for message stream
      if (req.url === '/stream') {
    
        res.writeHead(200, {
          'Content-Type': 'application/json'
        })
    
        // Stream messages 
        for (let message of messages) {
    
          res.write(JSON.stringify(message))
          res.write('\n')
    
          await new Promise(resolve => setTimeout(resolve, 1000))
        }
    
        res.end()
        return
      }
    
      return await serve(req, res)
    })
    
    server.listen({ port: 3000 })
    

    App.svelte

    <script>
      let messages = []
    
      // Connect to message stream
      const stream = new EventSource('/stream') 
    
      stream.onmessage = e => {
        messages.push(JSON.parse(e.data))
      }
    </script>
    
    {#each messages as message}
      <p>{message.text}</p>
    {/each}
    

    This creates a server that streams the sample messages and a Svelte component that connects to the stream and displays the messages.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.