-1

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

Someone
  • 21
  • 4
  • 1
    What "format" or "formats" are you talking about? Perhaps you can give some examples. – President James K. Polk Oct 16 '21 at 21:25
  • I originally used "code" instead of format but it would not accept my post, I will provide a couple examples – Someone Oct 16 '21 at 21:54
  • if you could write in the same way in all languages then you would have only one language. – furas Oct 17 '21 at 03:18
  • you may try to write own module for `websockets` and then you could write code more similar to JavaScript - but still there are other differences: JavaScript uses async all time and you don't have to run it manually, Python doesn't use `{ }` to group code but it needs indentations, JavaScript often use `lambda` function - like `function (event) {...}` - but `lambda` in Python has some restricitons. – furas Oct 17 '21 at 03:26
  • This is somewhat useful but I am not too concerned about language specific differences, more so that each library often adds or subtracts its own code, socket.io and ws are different even though they are both for node.js, My question is where either there is a library that is at least similar in how its written between languages, like your original suggestion, something more similar, I imagined its would have been made by now but I suppose learning each socket library is the only way, thank you. – Someone Oct 17 '21 at 16:39
  • It seems that the socket.io library does offer some good standardization between client and server and between languages, or at least as close as I will ever get. – Someone Oct 17 '21 at 18:57

1 Answers1

0

I have realized that those modules don't add "much" on their own and in fact just use the formatting of the language it self, albeit the examples are a little advanced

The general formula is

  1. import module or library (mostly language specific, some differences depending on module)

  2. start http server (language specific)

  3. mount module on top of http server (mostly language specific, some differences depending on module)

  4. send and receive information, usually in the following format, I will use socket.io as an example because its what I know but most use a similar format with change being mostly language specific

    //to send
    connection.send("recieve_this", information_being_sent)
    //to receive
    socket.on("recieve_this", function(information_being_sent){
       //receives (information_being_sent) and starts a function that you can do work on
    })
    

Here is an example of a full server running in javascript with socket.io

var http = require('http');               //important for hosting a server on http
const { Server }  = require("socket.io");//requires the imported socket module
const server = http.createServer(function(recieve, send){
}).listen(8000)                         //starts http server on port 8000 (can be any port you like)
const connection = new Server(server);  //creates a variable called connection and mounts socket.io's Server to http's server

//to send
connection.send("recieve_this", information_being_sent)
//to receive
socket.on("recieve_this", function(information_being_sent){
   //receives (information_being_sent) and starts a function that you can do work on
    })

This is all server code, you will need to sent client a script as well, read documentation.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Someone
  • 21
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 16 '21 at 10:57