Magier
Magier

Reputation: 517

node.js: create app that auto-updates clients by data-driven events

I have spend almost three days now on learning node.js. I went through an online webcast that cleared the basics. I checked out several samples and tutorials how to build simple apps like webserver, chat program, connecting to mySQL DB, using mongo DB with json...

So far so good. But reaching my goal is still impossible because those simple scenarios always just end when it starts getting intersting.

My destination is to build a demo project that is able to do the following:

1- hold a simple list of data objects serverside 2- show them on webpage of all connected clients 3- allow web-clients to modify the data objects 4- almost immediately updates all the other clients pages as soon an object was modified

(For the first step it would be ok to assume all the objects are once loaded and held in memory of the server (service?) - instead of polling the db for changes or subscrie any DB events - what seems to be another big X-Files topic people fill books about...).

Concerning 1 -3: I already realized using mongodb and json and some addd/edit/delete methods taken from the following walkthrough: http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/

Concerning 4: After looking at couple of chat app samples (e..g. http://nodecode.de/chat-nodejs-websocket) I thought this might be a good approach to solve this task, by just broadcasting the object that has been changed through socket (instead of sending a chat text message).

So I tried to combine these samples to one application that match my needs. But I failed. Even before wiring up the functionality I already stuck when I tried to place the two functionalities of showing/modifying the object list and provide the chat function just on one page side by side.

Maybe I am still missing some basics. Maybe the approach is wrong. I can't find samples for a similar task anywhere. So I guess now it's time to ask pros for some help where or how to start.

Thanks in advance.

Upvotes: 2

Views: 911

Answers (1)

Grandpapa
Grandpapa

Reputation: 173

You are on the right track. Websocket is the right approach.

"already stuck when I tried to place the two functionalities of showing/modifying..." Without reading your code, I can't really give you a detailed answer to the problem, but here is a vague high level summery of what I would do:

  • Create an api endpoint on the server-side for the client to read and update the data.
  • Figure out how to initiate a connection from server to client using websocket.
  • Send a notification using websocket to the client every time the api receives an "update" request.
  • Write a set of functions on the client side to fetch and render new data every time when a notification comes from server

Upvotes: 3

Related Questions