rocket-ws example that broadcasts one clients messages to all
- User
rocket-ws example that broadcasts one clients messages to all
Assistant
Sure, here's an example of a Rocket.ws server that broadcasts one client's messages to all other clients:
- First, you'll need to install Rocket.ws by running the following command:
npm install rocket-ws- Next, create a new file called
server.jsand 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.
- Now, create a new file called
client.jsand 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.- Finally, run the server and client scripts:
node server.js node client.jsWhen 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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.