"Pushing" images into a browser with nodejs - node.js

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.

Related

What happens when Coderpad creates an interview session?

I am trying to understand on a high level how a system like coderpad works. Everytime I use Coderpad to practice interviews with friends, it creates a session with a temporary link that both users can access to start the coding interview.
When a someone goes to the homepage they would be served the standard html page/client for the homepage. When they create an interview session they are served the html page/client for the coding pad, and there must also be a way to users to connect to the same session and for each session to be an isolated instance? Im guessing that when each user use the link, the server process their request and based on the link, it actually set up a stream connection between the users so that they can collaborate on a shared document, share video/voice.
my questions are:
- how exactly is the temporary link created, and how can it be created so fast?
- is my understanding of how it works correct?
- Giving topics to look into that could point me in the right direction would really help
I got curious about this too, after an interview on CoderPad.io. I suspect the temporary links are just for the server to identify the session - not actual pages on the server. Probably using WebSocket to communicate between the server and clients, broadcasting back to all users whenever code is changed (or other events.)
The coding pad page is the same static HTML. The contents and users are modified on the back-end, and only the results are shown - like in a chatroom.
Hope this helps.

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.

Sending and Receiving data from a website to NodeJs application

basically I am trying to figure out a way to make a connection between my Discord Bot and my Website, I have some ideas in mind in using GET and POST methods but IDK how to use them in NodeJs nor in the website as I am still new to it, so what I want is to send a packet of data from the website after a submit button and the bot which is hosted locally with the website will receive this data and work with it.
Express is a commonly used web framework for Node that takes care of routing fairly easily. You can see their documentation here.

NodeJS - Stream in what the server is doing

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.

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.

Resources