NodeJS - Stream in what the server is doing - node.js

I'm creating a simple webpage with NodeJS that'll upload a picture, resize it, pull some information from the web about the picture, and then save to the database. Easy stuff, all of it done server side. Though I'm trying to write an new feature that I'm a bit lost on how to go about. What I'd like to do is 'print' to the client when it's started each step of the process I mentioned about.
Imagine it like a white box, and every time something happens on the server, a new line is written says what for the client to read. How would I go about this? Any help is appreciated!

Use socket.io or some other websocket library. When the page loads, open a connection (in the browser's javascript). On the server, as events happen, send them as socket.io messages. In the browser, as events arrive, set them into a "status" element or append them to a list or whatever. You should be able to find lots of examples of chat servers out there and just convert chat messages to progress updates and there you have your architecture.
http://howtonode.org/websockets-socketio
Try something, then post a code snippet.

Related

How to change the content in real time using node js?

Have small app (My first Node js App), where I want add changing real-time content.
For example I have a common url which have list of content link, when user click on particular link it will open its correspondence link.
I have teacher and student role in the app so if teacher open the specific content(From the common url page) then it will automatically open that content for all student who have common url.
Please give me any Idea to implement this functionality and please tell me any plugin who have same functionality for real time content change.
One way, probably the easiest would be to use websockets, an easily manageable choice is socket.io npm package. What makes websockets different from http requests is the direction of the data flow. While http is pull based, meaning that a request has to be made to the server in order to get a response, websockets - once the connection is established - are or at least can be push based, meaning that the server could push out content without having to get a request from the client. In your situation, the teacher and the studens would all have an active websocket connection with the server. When the teacher clicked on a certain view, it would push the view’s data requirements to the students without any interaction from their side and the view would update on their screens according to the pushed data.
Look into how websockets work and try to experiment with a basic socket.io setup.

Update database before leaving a page in Node.js

I want to develop an attendance management page where I want to update my MongoDB database every time when the user leaves the page.
I found unload function in some answers, but cannot realize how to use it in Node.js.
How can I do this using Node.js framework in PhpStorm?
You would have to do it from server side, sending request from client before he leaves the page is a bad approach. There are many ways how client can disconnect without sending any request, like connection lost, killing process etc.
Maybe consider different approach, like sending an ajax request as an indication that user is still on page?
Or you could look around for existing solutions, maybe something like Using node.js to display number of current users
could help.

"Pushing" images into a browser with nodejs

I'm a newbie to nodejs. What that out of the way, here is what I'm trying to do. I'm working on a project requires to "push" images into the DOM of the browser as soon as they are appear in db.
Think about out it as Pinterest, but when new image is uploaded to Pinterest, it's automatically delivered to a browser, so user can seat there and watch new pictures pop-up on a screen.
I would appreciate any beginners guidance on the challenge.
There are two parts to this problem. The first is letting all of your servers know that you have a new image. The second is getting that information and image to the clients.
To tackle communication between your servers, I suggest looking into a pub/sub system. Redis can be used for this. Basically, the server that inserts a new image into the database will "publish" a message, which is then broadcast to all the servers that have "subscribed" to that type of message. Your message could contain the ID of the record you just inserted.
Once you have that in place, I recommend using Socket.IO to maintain a persistent web socket or web-socket-like connection between clients, and your servers. Your servers get the message that there is a new image, and then query the database for the details if needed. Once they have those details, they can send a message to the client(s) with the URL where the image can be accessed.

Node.js send variable to client

I have a small Node.js HTTP server that does requests to a mongo database (with the mongoose module).
What I want to do is query the database, store it in a variable (array) and send it to the client.
Because ideally, when the user clicks on one of the buttons on the html page, the JavaScript will pick-up the event and change the appearance of the website by showing calculations based on data that is stored in the database.
The only way I could come with was just "transferring" the database content to the client browser but if anyone can come with another solution that would be fine too !
So basically my question is :
How can I pass a variable from the Node.js server to the client browser when serving a page ?
Thank you in advance !
If you will be doing more than a couple of these types of transfers, I recommend looking into Socket.IO.
It's a layer that provides quick and easy communication between Node.js servers and web front-ends, by abstracting web sockets when available, and falling back to other transports (such as JSON-P or Flash) when it's not available. Basically, you would call io.emit('something', {yourdata: here}), and it is easily received on the other end. All of the serialization is done for you.
http://socket.io/
Give their demo a shot to see how it works.

Detecting an html button click with node.js?

I'm quite new to node.js, so my apologies if this is a potentially stupid question. I'm aware that when a new page is requested, it triggers a data event, which node can then handle accordingly; on my current project though, I have an html button that triggers a few events, but does not redirect the page. Because of this, node isn't recognizing that this is occurring. Is there any way for me to detect this? Or even, as a workaround, is there any way to call one of my node functions from the javascript embedded in the web page?
Thanks~
you are mixing up two different things, the data event in node which you are talking of is triggered because a network connection is established when you point the browser to your website, data is received and sent from the client to the server and back.
this has nothing to do with clientside ui events, for example a click event on a button.
if you want node to recognize a click on your button you have to send something to the server, you can either use a form, issue an ajax request or use websockets.
for ajax requests with jquery : jquery $.ajax documentation
a popular websockets implementation for node.js is socket.io
If you want to call something in Node from the client side, you can use socket.io to transfer events to Node. Then catch those events, and return the correct data back to the client. Refer to the socket.io "How to Use".

Resources