ArticleZip > Creating A Hello World Websocket Example

Creating A Hello World Websocket Example

Are you ready to delve into the exciting world of WebSockets and enhance your coding skills? In this article, we will guide you through creating a simple "Hello World" WebSocket example. WebSockets provide a bidirectional communication channel between a client and a server, allowing real-time data transfer and interaction. Let's get started by setting up a basic WebSocket server and connecting to it using a web browser.

First, you need to have a basic understanding of HTML, CSS, and JavaScript to follow along with this tutorial. If you are new to WebSockets, don't worry, we will explain each step in detail to make it easy for you to grasp the concept.

To start, create a new HTML file and add the following code to create a simple webpage:

Html

<title>Hello World WebSocket Example</title>


    <h1>Hello World WebSocket Example</h1>
    
        const socket = new WebSocket('ws://localhost:3000');
        
        socket.onopen = () =&gt; {
            console.log('WebSocket connection established');
            socket.send('Hello, WebSocket!');
        };
        
        socket.onmessage = (event) =&gt; {
            console.log('Message received: ', event.data);
        };

In this code snippet, we are creating a WebSocket connection to a server running on `localhost` at port `3000`. The `onopen` event handler logs a message when the connection is established and sends a message to the server. The `onmessage` event handler logs any incoming messages from the server.

Next, let's create the server-side code to handle WebSocket connections. You can use Node.js with the `ws` library to quickly set up a WebSocket server. Install the `ws` package by running `npm install ws` in your project directory.

Now, create a JavaScript file (e.g., `server.js`) with the following code:

Javascript

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 3000 });

wss.on('connection', (ws) =&gt; {
    ws.on('message', (message) =&gt; {
        console.log(`Received: ${message}`);
        ws.send('Hello, client!');
    });
});

In this server-side code, we create a WebSocket server using the `ws` library on port `3000`. When a client connects, we log incoming messages and send a response back to the client.

To run the WebSocket server, execute the following command in your terminal or command prompt:

Bash

node server.js

Once the server is running, open the HTML file in a web browser. You should see messages logged in the browser console indicating the WebSocket connection establishment and any messages received from the server.

Congratulations! You have successfully created a basic "Hello World" WebSocket example. This demonstrates the power of WebSockets in enabling real-time communication between clients and servers. Experiment with more complex functionality and build exciting applications using WebSockets. Keep coding and exploring new technologies!

×