Client-side Javascript server - possible? - javascript

Great day, community.
Question is next: is that possible to run simple HTTP server on client-side javascript which will be able to receive requests from global network and somehow process them?
For example, in a node.js I can run server with following code:
var http = require('http');
http.createServer().listen(3000, '127.0.0.1');
and then I'll have server running on 127.0.0.1:3000, I'm wonder to know is something similar to this can be implemented with a regular client-side javascript?

The definitions of "client" and "server" are relative. Node can be a server when it's sending data to clients; a Node app can also be a client to another server (for example, when you make an API call).
It sounds like you're asking if you can create a "server" using JS in the browser. You can't, but that's because most browsers are designed to only be clients, not servers — they can only make requests, not respond to them. In particular, Node itself connects to system-level sockets, which enable it to be a server. Browsers don't allow your Javascript code access to those system-level sockets, which is why it isn't possible.
Hypothetically, if they did, then you'd end up back at Node. Or recreating your own version of Node.
Note that you do have WebSockets in the browser; any other "client" can be on the "other side" of that socket. So you could implement a rudimentary client/server setup that way, but it wouldn't work with other HTTP clients.

Related

From javascript browser client-side to Postgresql [duplicate]

I have a c# tcp server, I want to connect to the server via a html client page.
The problem: There is no simple way to create TCP sockets in Javascript on a browser side. Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets. Not to any random servers that know nothing about HTTP.
so is there a solution to connect to my srver.
No. There just isn't. The browser is a tightly locked down environment. The only socket connection that you can open from JavaScript is WebSocket. Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.
Maybe someone else will have an idea for you, but...
The best solution I can think of is for your server to support websockets.
The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.
Bare in mind that Websockets can send and receive binary data. It's just that javascript make it more comfortable to write text based messages.
Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through. This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.

Getting data from client Nodejs

I made a simple server in nodejs. I want is to connect to the server from other application (app is in c++) on rasppserypi. I'm making connection and sending dataString from raspberry to node server.
So there is my question: How can i "catch" data that raspberry is sending to server?
You can use multiple options:
Make a simple http server.
Express is one of the most used modules for http servers in node.js. It is fast to setup and easy to use.
http://expressjs.com/en/starter/hello-world.html
Boost would be easy to use for the c++ part.
How to send http request and retrieve a json response C++ Boost
Make a tcp connection between them both.
You can also easily make a tcp connection between them both. Use Boost for the c++ part and the internal net module of node.js
https://gist.github.com/tedmiston/5935757
Use node.js on the raspberrypi as well. You can also run node.js from the raspberrypi and make the client server communication like in the snippet above. From node.js you can call your c++ program as a child process.
There are many other options but this should be the easiest ones. It also depends on your actual use case, what you want to build.

html javascript connect to raw socket

I have a c# tcp server, I want to connect to the server via a html client page.
The problem: There is no simple way to create TCP sockets in Javascript on a browser side. Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets. Not to any random servers that know nothing about HTTP.
so is there a solution to connect to my srver.
No. There just isn't. The browser is a tightly locked down environment. The only socket connection that you can open from JavaScript is WebSocket. Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.
Maybe someone else will have an idea for you, but...
The best solution I can think of is for your server to support websockets.
The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.
Bare in mind that Websockets can send and receive binary data. It's just that javascript make it more comfortable to write text based messages.
Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through. This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.

Can you have a Socket.IO (express) server be loaded from a web browser?

You know, a web server. Right now my Socket.IO server loads from a BATCH file that is a JavaScript file. Can you use node and make the socket.io server load from a web browser. Like a web-server utility tool or something of the sort.
That's explicitly not possible due to the design of WebSockets. It starts as a special HTTP request that, after the handshaking, drops the HTTP protocol and strips it down into the WebSocket protocol -- a nearly bare protocol similar to (but slightly more managed than) raw TCP. Because a web browser specifically cannot handle HTTP requests, it could never initiate the socket as a server.
This was done specifically so it wouldn't be possible to write a drive-by botnet website to use scores of users' computers for DDOS attacks without their knowing, amongst other security concerns.
So it wouldn't surprise me if Flash supported that kind of behavior. ;) (I know Java can, but who enables Java applets?)
I'd say you Can. Not that I can think of a good use case.
You would need to put the startup code somewhere where the web server could run it and you would need to get the web server to return some information to the browser to allow it to then connect. You would also have to insert the socket.io code into the browser after the socket server had started.
So I Think that it would indeed be possible but rather complex for little gain. I suppose one possible use case would be to restart a socket server after failure. Actually I'd do that a slightly different way, probably by calling an external script from Node.
fortunatly the answer is no. if you mean by load / launched , NO. but you can create a script on a server that launch another server once a url is requested by a a client.

How to notify client's browser about some event on server?

I'm using python (Tornado) on server side and some javascript on a client side. I have the common situation - one user send a message to another. And I want server to notify client's browser (reciever of the message) about new message. How can I do it? Should I establish long-alive connection with client (maybe using websocket) or something else?
PS
For establishing conenction via websocket I found good library TornadIO
PS2
So, due to high load of project establishing connection betwebb server and each client looks suspicious. I afraid of c10k problem. May be it's only lack of my knowledge.
I'm using python (Tornado) on server side and some javascript on a client side. I have the common situation - one user send a message to another. And I want server to notify client's browser (reciever of the message) about new message. How can I do it? Should I establish long-alive connection with client (maybe using websocket) or something else?
Using a realtime web server such as Tornado - yes. TornadIO looks like a good solution since it'll use socket.io which has fallback options for older browsers.
The Wikipedia entry on the C10k issues provides a list of servers that have resolved this problem:
A few web servers have been developed to counter the C10K problem:
nginx, which relies on an event-driven (asynchronous) architecture, instead of threads, to handle requests (WordPress.com uses nginx to solve the C10K problem)[2]
Lighttpd, which relies on an asynchronous architecture to handle requests[3]
Cherokee, a lightweight web server[4]
Tornado, a non-blocking web server and web application framework[5] written in Python
Apache Deft, asynchronous, non-blocking web server running on the JVM
JBoss Netty, a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients[6]
Node.js, asynchronous, non-blocking web server running on Google's V8 JavaScript engine[7]
EventMachine, an asynchronous, non-blocking web server running on Ruby EventMachine
Yaws, a web server written in Erlang; profiting from Erlang's extremely lightweight processes.
Medusa, a non-blocking web server library written in Python
As you'll see, Tornado is listed.
Beyond this your question is potentially more about horizontal scaling than it is about how to achieve server to client notifications.
Depending on which realtime server solution you choose this might never become an issues. For example, a single instance of Caplin System's Liberator can achieve a lot more than 10,000 persistent connections.

Categories

Resources