M
M
Maxim Zolotoy2020-05-03 12:33:21
Node.js
Maxim Zolotoy, 2020-05-03 12:33:21

How to configure everything on VDS for websocket requests on node js?

I decided to learn node - I want to figure out how to make a websocket server. Help me understand how it all works, please.
And do not judge strictly, apparently I do not have enough understanding about client-server interaction, so my questions may seem strange to you.

Here I developed the application locally, I run it and now my sockets work, for example, on localhost: 3000
And from any local site I can connect to it from js.

And here are the questions when this thing happens on hosting.
1) It turns out that sockets will be available on the VDS ip address and the specified port?
I don't quite understand this point. And if VDS has a different ip for each site - how can I understand which sockets will start on?

2) How to start the server process and make it so that you can close the console and it automatically restarts when it crashes and starts when the VDS is

rebooted url is defined - and if url is possible - then where is it all configured. I mean at what url the socket will be available.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vreitech, 2020-05-03
@fzfx

> It turns out there sockets will be available on the ip address of the VDS and the specified port?
Yes.
> And if VDS has a different ip for each site - how can I understand which sockets will start on?
which IP address you specify in the config - they will be on that.
> How to start a server process and do so
write a unit for systemd. more information - on request "unit for systemd".
> In order to connect to websockets from a js script - you need to specify exactly ip and port
, you need to specify the URL.
> where it is configured.
somewhere in the application or in its config. I won’t specify exactly - I’m not familiar with node.js.

D
Dmitry Belyaev, 2020-05-03
@bingo347

Normally, the node should not stick out, you're probably proxying the rest of the api through nginx, right? You can simply make a proxy for a specific location on the port where it is raised, something like this:

location /ws {
  proxy_pass http://localhost:3000;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "Upgrade";
}

How to start the server process and make it so that you can close the console and it automatically restarts when it crashes and starts when you reboot VDS
pm2

S
Somewhere Intech, 2020-05-03
@john36allTa

Websockets work on the basis of an http/https server, so you can allow access to them from anywhere. The technology specification provides for a mandatory origin check, i.e. url from which the connection is made, and the protocol is a string parameter. Origin, by the way, can be faked.
Connect ws(s)://fqdomain.name:{port: number}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question