WebSocket Chat Application is a simple chat server built using the Jakarta WebSocket API. This application allows multiple clients to connect and communicate in real-time through WebSocket connections, enabling an interactive chat experience.
-
Java: The programming language used for the application.
-
Jakarta WebSocket API: Provides the WebSocket functionality.
-
Open Liberty: The server runtime environment for deploying the application.
Before you begin, ensure you have the following installed:
- Java Development Kit (JDK) version 11 or higher.
To set up the application, follow these steps:
-
Clone the repository:
git clone https://github.com/sweettypdevassy/ws-blog-backend.git cd ws-blog-backend
-
Run the project using Maven:
mvn liberty:dev
Once the application is running, you can test the chat functionality:
-
Open two browser windows.
-
Use the browser console as the WebSocket client. In Chrome, press F12 and navigate to the Console tab.
-
Connect each tab to the WebSocket server by running the following JavaScript code:
const ws = new WebSocket("ws://localhost:9080/ws-blog/chat"); ws.onopen = () => console.log("Connected to chat"); ws.onmessage = (msg) => console.log("Message from server: " + msg.data); ws.onclose = () => console.log("Disconnected from chat");
-
Send a message to the chat using the following function:
function sendMessage(text) { ws.send(text); } // Example: To send a message, type sendMessage("Hello from user!");
-
In each console, use sendMessage("Your message") to send messages.
-
You should see messages from both users appearing in both browser consoles, creating a live chat experience.
Contributions are welcome! If you have suggestions for improvements or new features, please fork the repository and submit a pull request.