Search code examples
javascriptsocketsserverclient

What would be the most simple way of establishing a client-server connection in Javascript?


For someone like me who has little experience with sockets or client-server connections. What would be the easiest/simplest way of establishing a client-server connection:

An example of what I mean:

Server:

var server = server.create(28001);

server.onConnect(console.log("Connected"));

server.send("Message");

Client:

var client = client.create();

client.listen(28001);

client.onRecieve(console.log(client.receivedMessage));

And I'm just using regular js, not Node.js or others.


Solution

  • You should know some basic knowledge about how javascript works. You can refer to this paragraph - How JavaScript works: an overview of the engine, the runtime, and the call stack

    If you want to run a .js file, you need JS engine and runtime. Chrome and Node.js are both these thing. That's why you can run JS code in Chrome console or with using node command. The network part is always supplied by the runtime.

    Regular JS is a JavaScript framework or library. You need an environment to run the code. Regular JS doesn't target to writing server-side code, so there is no such library. Although browsers have runtime, I don't think the runtime contains any function you can use to LISTEN on a port, which is necesary to a server.

    In one word, the easiest way to establish a server is still using nodejs.