Fetch net server from browser - node.js

Is it possible to fetch NodeJS Socket server from web browser ?
I've create net server and i would like request this server from web client :
const server = net.createServer();
server.listen(SERVER_PORT);
I've try to fetch with ip and port, but no result.
Anyone have idea ?

This is not possible.
From web browser, you can only communicate through HTTP or WebSocket.
If you try to talk to a TCP server with the wrong protocol, it'll error out.
If you absolutly want your server to be a net TCP server, you can implement a server that communicate with your web app through a WebSocket and forward messages to your net server.
Otherwise, you could just do a WebSocket server, with socket.io for example.
Hope it helps,
regards

Related

Difference in server port and websocket port in node.js chat application

I am trying to create a multi room chat application in node.js using socket.io and express. I am confused between use of server port and websocket port. I understand server port is used by the client to connect to server. But not sure about use of websocket port.
Thanks & Regards..
webSockets can share the same port as your web server and this is a common configuration. The reason this works is because of how a webSocket establishes a connection (all webSocket connections are initiated with an HTTP request). It works like this:
Client makes an HTTP request to a web server with a header specifying that they want to "upgrade" to the webSocket protocol and sends a security-related header.
Web server sees the upgrade request and, if it has support enabled for webSocket connections, it will respond with a 101 request (switching protocols) and another security related header.
Client gets the accepted upgrade and both ends switch to the webSocket protocol and the original TCP socket that started out using the HTTP protocol is now using the webSocket protocol.
In this manner, the same port and webServer can be used for regular HTTP requests or webSocket connection requests.
For a chat application it is common to use a webSocket connection because it is a continuous connection that more easily allows the server to send information directly to the client which is often needed in a chat application.
To understand more about how a webSocket connection and server work, see this reference on MDN: Writing WebSocket servers which shows the step by step process for initiating a webSocket connection.
Server socket is used by server... that keeps listening to coming sockets request in a loop... and websocket sends a request to server socket and bound a connection between two devices...
If you have / want to have web clients, WebSocket is going to be required, because there is no access to 'regular' TCP (or UDP) sockets from browser-based JavaScript (and I assume you do not want Flash, SilverLight or Java Applets, in 2017). WebSocket is not special because of the port number, but it is special because of the protocol: a WebSocket connection starts as a regular HTTP connection, and protocol upgrade reconfigures it afterwards, it is designed for the browser-world, and even capable of traversing HTTP proxies. After establishing the connection, it provides a full-duplex, bi-directional message stream, very usable for chat applications.
And because of being a Web-thing, you could simply use port 80, if you are allowed to.

SocketIO on port 80 together with Express

I have ExspressJS app run with Socket.io, due to firewall issues with higher port for SIO i want to switch that both will work on port 80.
Found this small article and on my dev machine it's look working good.
My question is, is it really goo to do that? is it a good practice? if not why?
Please advise.
It makes absolute sense to run socket.io and your web server on the same port.
The webSocket protocol (which socket.io is based on) is specifically designed for this to be the primary way that socket.io is used for a bunch of reasons including same-origin permissions and client and server firewall routing of port 80.
In case you didn't realize it, every socket.io connection starts with an HTTP request to a specific route and then once the initial handshake between client and server has been confirmed, then the protocol is "upgraded" from HTTP to webSocket. Because all socket.io connections connect in on a very specific route, all other HTTP connections can easily be separated out and be treated by your web server as regular web requests.

Connect to Socket.io server with standard web-socket client

Is it possible connect to socket.io server with standard web-socket client?
When connecting to socket.io server through socket.io client, Url started with HTTP but for web-socket clients must use ws.
so, if it is possible, how to connect to socket.io server and what is the url for connecting to socket.io server?
socket.io has a protocol on top of webSockets. If you're going to use socket.io on the server side, then you have to use a socket.io-compatible client on the other end.
So, you can't use a plain webSocket client to connect to a socket.io server.
The socket.io protocol is here.

flash socket policy with nodejs and expressjs

I have small application on nodejs + expressjs + socket.io and it works like a charm... mostly.
There is problem with flash socket policy server. Thing is that I cannot run socket policy server on port 843. If Flash Player cannot retrieve a master policy file from port 843, then it requests a socket policy file on the port where it is trying to connect. So the solution could be on 80 respond to normal http request (with express) and on the same port catch plain TCP connection with data <policy-file-request/>\0 and respond with policy. I was trying adapt this code but without luck.
How force express to respond with policy for TCP connection when <policy-file-request/>\0 is incoming and serve http response otherwise?
I don't know if this is possible with the socket.io Flash movie, but you could explicitly set a port to load the policy file from.
From the point of an ActionScript 3 developer you could do:
Security.loadPolicyFile("xmlsocket://example.com:10843");
socket = new FlashSocket("example.com:80");
This way it is possible to load the crossdomain.xml from a other port then your HTTP port or 843.

Connecting to Nodejs/Socket.IO server with SSL (https) when listening to custom port

My socket.io server is running and listening to port 6060.
Now, I added to my domain an SSL certificate in order to serve my website through https protocol.
If i'm trying to access the nodejs/socket.io server through http://mydomain.com:6060 i'm getting the "Welcome to socket.io." message, but when i'm doing it with https://mydomain.com:6060 it's not work..
Is it posible to connect to my socket.io server (listening to port 6060) with ssl?
What are my options in such a case?
Thanks
After searching on sockeio docs i found this:
https://github.com/LearnBoost/socket.io/issues/1011
Try this:
var socket = io.connect('https://mysecuresite.com',{secure: true, port:6060});

Resources