Skip to content
TheChatArchiveThe Chat Archive
Metallama-2-70b-chat· MetaHugging Face Arena Human Preference 55K

rocket-ws example that broadcasts one clients messages to all

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

    rocket-ws example that broadcasts one clients messages to all

  2. Meta Assistant

    Sure, here's an example of a Rocket.ws server that broadcasts one client's messages to all other clients:

    1. First, you'll need to install Rocket.ws by running the following command:
    npm install rocket-ws
    
    1. Next, create a new file called server.js and add the following code:
    const Rocket = require('rocket-ws');
    
    const server = new Rocket.Server({
      // Broadcast messages to all clients
      onClientMessage: (client, message) => {
        server.broadcast(message);
      },
    });
    
    server.listen(8080, () => {
      console.log('Server listening on port 8080');
    });
    

    This code creates a new Rocket.ws server and sets up an event listener for when a client sends a message. When a client sends a message, the server broadcasts the message to all other clients connected to the server.

    1. Now, create a new file called client.js and add the following code:
    const Rocket = require('rocket-ws');
    
    const client = new Rocket.Client('ws://localhost:8080');
    
    client.on('open', () => {
      console.log('Client connected');
    
      // Send a message to the server
      client.send('Hello, server!');
    });
    
    client.on('message', (message) => {
      console.log(`Received message from server: ${message}`);
    });
    
    client.on('close', () => {
      console.log('Client disconnected');
    });
    

    This code creates a new Rocket.ws client and connects to the server at ws://localhost:8080. When the client is connected, it sends a message to the server and listens for messages from the server.

    1. Finally, run the server and client scripts:
    node server.js
    node client.js
    

    When you run the client script, you should see the message "Client connected" in the console, followed by the message "Received message from server: Hello, server!" When you send a message from the client to the server, the server broadcasts the message to all other clients

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