It seems that every single language and all of its web socket libraries uses its own slightly different quirky method to write its web socket code, slightly different code, slightly different language, some longer, some shorter, some simpler and some harder, but there is no consensus, is there a way to make my python and node.js web socket code server the same, and make them equal to browser's inbuilt socket, or must I learn each different code?
Examples: 1: Python with asyncio
import websockets
# create handler for each connection
async def handler(websocket, path):
data = await websocket.recv()
reply = f"Data recieved as: {data}!"
await websocket.send(reply)
start_server = websockets.serve(handler, "localhost", 8000)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Example 2: Node.js with ws
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
enter code here
Client side example:
const socket = new WebSocket('ws://localhost:8000');
socket.addEventListener('open', function (event) {
socket.send('Connection Established');
});
socket.addEventListener('message', function (event) {
console.log(event.data);
});
const contactServer = () => {
socket.send("Initialize");
The issue is that they are all so different, is there a way to solve this problem