Getting data from client Nodejs - javascript

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.

Related

Using Socket or Http Request passing data different node.js servers

Everyone that's a bit of a strange question.
I have a product(a Node.js Backend) that's I sell and people deploy it by themselves.
And my goal now is to make them sending data to each other if they want.
they are deployed on private servers.
The data is simple text(writing some text send it to other servers).
it needs to be end-to-end encrypted.
It's not a flow of data in continue(chat Room).
they should have the possibility to send to multiple different servers.
I don't want to use a third-party server to host everything and save the data(they have their own MongoDB Local).
And I don't really know where to start and what to use.
My first problem is do I use regular HTTP requests or WebSocket or any other technology?

Socket server on client side js?

I have node.js server, and I need to create dynamically updated web page with updating data. So, I thought that sockets are way to go.
But there's one problem. I need to send data from the server to client(to browser).
From my research, it is not really possible to create socket server with the client side JS. It is easy to do it the other way, but to send data only from server to client?
What would be best and easiest way to do that?
You create a webSocket or socket.io connection from your client to your server and your server is the webSocket or socket.io server. Once that connection is established, you can then freely send data either way across the connection, from client to server or from server to client.
This type of architecture is ideal for sending data from your server to a web page to dynamically update the web page as new data arrives.
webSocket is the base transport. socket.io is a layer on top of webSocket that adds a bunch of useful features such as auto-reconnect and structured messages. You can use either from a browser. webSocket support is built-in to the browser. If you want to use the additional features of socket.io, then you include the socket.io client library in your web page.
Here's a listing of some of the additional features socket.io offers over a plain webSocket: Moving from socket.io to raw websockets?.
I am not sure I have fully understood your question.
But, if I got it correctly, in order to have a "socket connection" you need to have two sides - a server and a client.
Use socket.io lib with a lightweight node.js server.
You can take a look at their docs + examples - will be very straight-forward.
If you still having trouble, write.

How to get data from node.js server into Electron app?

My question is about:
I am currently trying to connect my electron application to my node server to get data from node.js server and print that data to my electron application. However I don't know how to do it. So could anyone help me:
To get data from node.js server into electron app?
The way you get data from any node.js server is you make a request to that server specifying in the request what you're asking for and the server responds with the appropriate data.
There are literally thousands of ways to physically make the request. The classic way in these days of web technologies is to make an http request from your electron app to an http server in your node.js server. You can make such a request from electron using the request() library.
You would then have an http server as part of your node.js server and you'd specify routes in that http server that handle the requests your electron client is making, fetch the desired data and send the data back as the response. In the node.js world, you can create a simple http server and a few request handlers in a dozen lines of code using the Express library.
This is the general approach. Further details on the exact request to make and URLs to use are dependent upon the details of what you're trying to do and the design you choose, none of which you've shared with us.

Client-side Javascript server - possible?

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.

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