realrandolph / autochat

a minimalist websocket-based chat page

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

websocket-server

This is a backend websocket server in JavaScript that handles user connections, shares state, and passes user messages on to a queue as well as broadcasts user messages to other connected users. Prerequisites

Node.js

Installation

Clone the repository

git clone https://github.com/your-username/websocket-server.git

Install dependencies

npm install

Usage

Start the server

node index.js

Write a client to connect to the server

Here is an example of a client in JavaScript that connects to the server and sends and receives messages:

const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:8080');

ws.on('open', () => {
  console.log('Connected to server');

  // Send a message to the server
  ws.send('Hello, server!');
});

ws.on('message', (message) => {
  console.log(`Received message from server: ${message}`);
});

ws.on('close', () => {
  console.log('Disconnected from server');
});

This client connects to the server at ws://localhost:8080, and whenever it receives a message from the server, it logs the message to the console.

About

a minimalist websocket-based chat page


Languages

Language:JavaScript 67.1%Language:CSS 22.0%Language:HTML 11.0%