0

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.

  • First you have to figure out what you want to connect to. This could be node, php, java, etc. and then they can be running as an HTTP server that accepts GET, POST, PUT, etc. Or you could run it as a web socket server and connect to that https://developer.mozilla.org/en-US/docs/Web/API/WebSocket – Matt Jun 30 '21 at 03:21
  • How would I go about putting in the URL with just the js files on my hard drive? –  Jun 30 '21 at 03:39

1 Answers1

0

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.

Jason Pan
  • 702
  • 7
  • 21
  • I'm so sorry, I didn't know regular js was an actual library. What I really meant was I'm just running js through a –  Jun 30 '21 at 04:10
  • @Ryker404 Thanks for your acception. It's a dangerous action that the ports can be opened by requested script, so browers won't start to listen a port (except some plugins). – Jason Pan Jun 30 '21 at 04:20
  • How would I send and receive information from the HTML page, take for instance a simple chatting webpage. I want to try to do this without having to install additional libraries or at least keep them to a minimum. –  Jun 30 '21 at 04:28
  • @Ryker404 If you just want send-receive, use jQuery is enough. https://stackoverflow.com/a/247511/3801587 – Jason Pan Jun 30 '21 at 06:15