DeveloperBreeze

Simple WebSocket Server using 'ws' library

// Import 'ws' module
const WebSocket = require('ws');

// Create WebSocket Server on port 8080
const wss = new WebSocket.Server({ port: 8080 });

// Event listener for new connections
wss.on('connection', (ws) => {
  // Event listener for incoming messages
  ws.on('message', (message) => {
    console.log('Received:', message);
    // Send a response back to the client
    ws.send('Server: ' + message);
  });
});

Continue Reading

Discover more amazing content handpicked just for you

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

Protect your API from abuse and learn how rate limiting works under the hood.

When developing web apps or APIs, it’s critical to prevent users from overwhelming your server. That’s where rate limiting comes in. In this guide, we’ll build a custom rate limiter in Node.js using Redis—no libraries, no magic, just code you control and understand.

Apr 04, 2025
Read More
Tutorial
python

Build a Multiplayer Game with Python and WebSockets

pip install flask websockets asyncio

Let’s first create the game logic for tic-tac-toe.

Dec 10, 2024
Read More
Tutorial
bash

How to Reset the MySQL Root Password Using DROP USER

Run this command to stop MySQL:

sudo systemctl stop mysql

Oct 03, 2024
Read More
Code
bash

How to view free space on a Linux server

  df -h /home
  • Inodes: To check free inodes instead of disk space, use the -i option:

Aug 11, 2024
Read More
Tutorial
bash

Finding the Top 10 Biggest Files on an Ubuntu Server

sudo du -ah /path/to/directory | sort -rh | head -n 10

This command lists the largest files and directories in the specified path.

Aug 11, 2024
Read More
Tutorial
javascript css +1

Building a Real-Time Chat Application with WebSockets in Node.js

body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f9;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
}

.chat-container {
    background-color: white;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    width: 300px;
}

#messages {
    list-style-type: none;
    padding: 0;
    max-height: 300px;
    overflow-y: auto;
    margin-bottom: 10px;
}

#messages li {
    padding: 8px;
    background-color: #f1f1f1;
    border-radius: 3px;
    margin-bottom: 5px;
}

#chat-form {
    display: flex;
}

#message {
    flex: 1;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 3px;
    margin-right: 5px;
}

button {
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 3px;
    padding: 8px 12px;
    cursor: pointer;
}

button:hover {
    background-color: #0056b3;
}
const socket = io();

const messageForm = document.getElementById('chat-form');
const messageInput = document.getElementById('message');
const messagesList = document.getElementById('messages');

messageForm.addEventListener('submit', (e) => {
    e.preventDefault();
    const message = messageInput.value;
    socket.emit('chatMessage', message);
    messageInput.value = '';
});

socket.on('chatMessage', (message) => {
    const li = document.createElement('li');
    li.textContent = message;
    messagesList.appendChild(li);
});

Aug 03, 2024
Read More
Code
php

JWT Token Creation and Verification in Node.js using 'jsonwebtoken'

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Simple HTTP Server in Node.js

// Import 'http' module
const http = require('http');

// Create a simple HTTP server
const server = http.createServer((req, res) => {
  res.end('Hello, Node.js!');
});

// Start the server and listen on port 3000
server.listen(3000, () => {
  console.log('Server is listening on port 3000');
});

Jan 26, 2024
Read More
Code
javascript

Read and Write Files in Node.js using 'fs' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Simple RESTful API in Node.js using Express

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Date Manipulation and Sum Calculation

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Access Command-line Arguments

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Set and Access Environment Variable

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Event Emitter using 'events' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Construct File Path using 'path' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Basic Authentication using 'express-basic-auth' middleware

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Create and Print Buffer

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Hashing Password with SHA-256 using 'crypto' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Parse URL and Query Parameters

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Execute Shell Command using 'child_process' module

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!