// 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);
});
});Simple WebSocket Server using 'ws' library
javascript
Related Posts
More content you might like
Tutorial
Build a Custom Rate Limiter in Node.js with Redis
REDIS_URL=redis://localhost:6379// redisClient.js
const redis = require("redis");
const client = redis.createClient({ url: process.env.REDIS_URL });
client.on("error", (err) => console.error("Redis error:", err));
client.connect();
module.exports = client;Apr 04, 2025
Read More Tutorial
python
Build a Multiplayer Game with Python and WebSockets
Serve the game interface using Flask.
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)Dec 10, 2024
Read More Tutorial
bash
How to Reset the MySQL Root Password Using DROP USER
DROP USER 'root'@'localhost';This removes the existing root user, including any potential issues related to password or privileges.
Oct 03, 2024
Read More Code
bash
How to view free space on a Linux server
- Inodes: To check free inodes instead of disk space, use the
-ioption:
df -iAug 11, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!