Expose socket via web sockets - javascript

I have a server that creates a socket on port 8181. I would like to access that socket from a web page opened in Google Chrome 14. I suppose it is not possible in a straight manner. Chrome provides support for Web Sockets but not for standard sockets. Is that right?
Is is possible to somehow create an intermediary that would expose my socket server listening on port 8181 as a web socket server running on some other port?

websockify is a generic WebSockets to TCP socket proxy/bridge. I created websockify originally to allow noVNC (HTML5 VNC client) to be able to connect to an unpatched VNC server. But it is generically useful (not VNC specific) so I spun it off as a separate project.
On a UNIX/Linux system you would run websockify like this:
./websockify 8080 my_server:8181
Port 8080 in the above example is the port to listen for WebSocket connections. my_server is the name/IP of your system where you have a server listening on port 8181. If you are running websockify on the same system as your server then you can just use localhost in place of 'my_server'.
The websockify project also comes with a Javascript wrapper library called websock.js. websockify supports the new protocol versions (used by Chrome 14+ and Firefox 6+) but it also works with older versions. However, with the older versions of the protocol there is no way to send raw binary data (only UTF-8), so websock.js and websockify work together to base64 encode/decode the data (between the browser and websockify) when using the older protocol so that you can still send raw binary data to/from the target.

Related

Node Server With NGINX

I have node js server that has a server which listens 8000 port and a socket.io connection working on that server. This socket connection creates a communication with a ReactJS app which is not a point of this question. So I have 2 project folders
1. project-server
2. project-web-react
Project server only answers socketio request and does not render a HTML or something else. It only works on terminal. I want to ask whether is it useful to encapsulate my project-server with Nginx? So the requests are handled by Nginx ? Or is it out of the Nginx's purpose?
I would never have an application server run directly connected through internet since there are always a bunch of unknowns with them (scaling, standard compliance etc), so I would recommend you to run a proxy like nginx in front of your app. This also makes it easy to add certificates and do load balancing / caching. It just adds flexibility and some security.

Cannot connect to mosquitto broker from local web client on windows

I am trying to connect to mosquitto windows broker service on my machine through javascript. I understand that Paho javascript client connects only through websockets and mosquitto for windows doesn't support websockets unless you build it yourself. My question is that since I am trying to connect to localhost should it matter to have a websockets enabled mqtt broker? Is there any way to connect to a local mqtt server without using websockets through javascript? And is it unsafe to do so or is it fine since I am connecting to a local server itself?
Thanks in advance for the answer and sorry if it sounds lame to you. I am extremely new to this space.
Even when connecting to localhost you still need websockets support to use the paho JavaScript support.
There are some small apps that will set up a separate websocket listener and bridge that to pure native MQTT e.g.
http://hai-ng.blogspot.co.uk/2014/08/setting-up-mqtt-websocket-gateway-with.html

Azure Web Site starting my Hapi Node.js site with socket protocol

Whenever I deploy my Hapi.js web application to azure, it starts the server using the socket protocol (see output below).
socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb
GET /
GET /{param*}
GET /api/employees
POST /api/employees
GET /api/employees/{id}
PUT /api/employees/{id}
DELETE /api/employees/{id}
POST /api/worklog
GET /login
POST /login
Hapi server started # socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb
150914/214730.270, [response], socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb: [1;32mget[0m / {} [32m200[0m (316ms)
However, whenever I am running this locally, it starts using http... I have not run into this issue using express or loopback, only Hapi. Is there some sort of configuration that I am missing? This is the server.connection function:
var server = new Hapi.Server();
var host = process.env.host || '0.0.0.0';
var port = process.env.port || 3000;
server.connection({host: host, port: port});
The reason this is a big deal is because I cannot pass socket://*<mydoamin>* to google as a callback URI for OAuth.
You shouldn't need to pass socket://<domain> to google, you'd pass the normal https://yourDomain.com or even the https://yourSiteName.azurewebsites.net to Google for OAuth callback and it should work as you would expect.
The fact that the node application is listening on a pipe rather than a normal tcp socket is just an implementation detail of iisnode. Basically the problem is that node has it's own webserver so you can't use it with other webservers like IIS, Apache, nginx, etc. iisnode bridges the gap between IIS and node in that it allows IIS to listen to the HTTP port on the machine 80 and when IIS gets a request on that port, it just forwards it to the node process that's listening on a named pipe. This allows you to manage your sites in IIS as you normally would on a Windows Server machine, while actually writing your app in node.
You can think of it as 2 webservers running on the box, one (IIS) is acting as a proxy for the other (node) where all the work is actually happening. The fact that the iisnode developer chose to use a named pipe instead of a normal tcp socket is odd (though kind of understandable since you can't easily reserve a port per se as you can a pipe), but it's the way it is.

making node.js net client

In all the examples of creating a client for node.js built-in net module I don't see how they get net to run on the client with out downloading it??
surely if my client-side code starts like this:
var net = require('net');
var client = net.connect({port: 8124},function(){
console.log('client connected');
client.write('world!\r\n');
});
then first i must write:
<script src="some strange node path to net ??"></script>
am i correct? how do I do this? btw: node.js is installed in my root on my server not local
I believe there's a bit of confusion here. The client example code you're showing is meant to run in node, not in the browser. That makes node a client of another (node or otherwise) TCP server.
If you want node to be the client and connect to another server over TCP, then run the code you're pasting. The net module is bundled with node, you're good to go.
If, however, you want your browser to talk to a node server then this would have to be over websockets (a streaming binary protocol over http, this is not plain-vanilla TCP). You would have to have a websockets module in the server, not plain net.
Just to be clear: net is simply node's interface to TCP sockets.

Dotcloud www and TCP in single app -

I'm trying to get a nodejs socket server running that will allow remote communication between two clients running a Flash game that communicates using a custom protocol. Due to Flash security restrictions, it seems that the socket server must be running on the same host as the web server that servers the Flash game. I've been continuously getting the following error:
The service crashed at startup or is listening to the wrong port. It failed to respond on port "nodejs" (8080) within 30 seconds
What I need is a way to run my nodeJS server code, while simultaneously serve the flash files.
I'm using the environment JSON variables to determine what port to listen on, and my YML is similar to the one discussed here but no luck...
Just wondering if I can get some info on how to create a working socket server/web server that will work for this (or if it is actually possible)
You can use the following dotcloud.yml file:
www:
type: nodejs
ports:
mything: tcp
Then in your Node.js app, you can bind a HTTP server to port 8080, and an arbitrary TCP server to the port contained by environment variable $PORT_MYTHING. Then run dotcloud info on your service; in the ports section, you will see something like this:
- name: mything
url: tcp://myapp-johndoe.dotcloud.com:12345
From now on, if you connect to myapp-johndoe.dotcloud.com on port 12345, you will actually connect to $PORT_MYTHING in your application.
I hope that it makes sense, and that it is what you were looking for!

Categories