1

I'm new in this subject so this might be a silly question for most of you. I have a simple server which several users will access. If any of them change a CSS property of an element, the others should be able to see the change in real time.

Should I use something like node.js to perform this? How do I save the changes the users do?

The page would look something like this: http://stom89.dyndns.org/

Thanks!

Tomas
  • 340
  • 1
  • 5
  • 14
  • The server will control switches that turn on/off different devices using a Raspberry Pi. – Tomas Jun 17 '12 at 23:57
  • @Tomas just to clarify what you are truly asking for... are you trying to push info to users in real time? (which is commonly a very hard task and requires you to almost own your own server) or would you be fine with data being check by the users browser every so often? – gabeio Jun 18 '12 at 08:38
  • @gabeDel, I have my own server. It is the raspi! Pushing info may be something I'll implement later. – Tomas Jun 18 '12 at 12:13
  • kk hope it works out sounds like a cool project keep it copyleft :] – gabeio Jun 18 '12 at 17:05

3 Answers3

2

I think you would need to use a sql database and have a javascript to detect changes and update through AJAX. That's my best idea.

Westwick
  • 2,367
  • 3
  • 28
  • 51
2

I guess what you want to change in your CSS / html , are states. Like if a lamp is on/off? Then you need to save each state in a mySQL DB and just grab the data for each user. If you want it to look like realtime for online users, then use js(ajax) to sync data regularly.

Alternative way without a DB would be with files.

If you don't wanna use mysql for this, you can use files. I suggest using ini files. For more on how to read/write ini files, you can visit this question. It's super simple and you'll be able to have each variable in a nifty array.

What you need: A bit of PHP, a little bit of jQuery (or js), understanding of GET variables

I suggest you create 3 files.

index.php :
Your main page which is the client. Pulls info using get variables. You can use jQuery.get() for this.

getstate.php :
This is the file which will read the ini file and give you back the states for each device. Read them with jQuery.get() from index.php .

savestate.php:
This is the file which you'll send the new states to from index.php Example request: http://address.goes.here/savestate.php?bedroomlight=1&garagelight=0

Whats even more interesting is that ini files can be written/read easily by many programming languages so you can manipulate the data using your Raspberry Pi easily. (say someone turns of a light, a script polling state could change the ini file)

Community
  • 1
  • 1
ioannis
  • 337
  • 3
  • 10
  • Exactly, I want to change the states. Can I do this with only javascript? – Tomas Jun 18 '12 at 00:18
  • Short answer no. You need to save states serverside, which JS cannot do. With PHP, JS, and files though you can. Check my revised answer above. – ioannis Jun 18 '12 at 08:28
  • Come to think of it, you don't necessarily need ini files to do this. A simple text file with comma separated values would do. Guess .ini is neater/cooler to use. :P – ioannis Jun 18 '12 at 09:19
  • Thanks for clearing the situation out. I also thought, using a DB is too much so a plain file is better. – Tomas Jun 18 '12 at 12:09
1

I have been messing with this subject for sometime if I completely understand your question. I would suggest looking at python, ruby or node.js though I could not say which is the easiest to learn for you though I would suggest python and a comet server which could be ape and simply have the server push the updates to the users that are already on the site.

Edit: Suggestions for polling :: jQuery
http://api.jquery.com/jQuery.get/ for standard data retrieval which is about all you will need.

gabeio
  • 1,282
  • 1
  • 23
  • 37
  • I think node.js is overkill for such a simple application, some may argue that mySQL is also, thus letting us to the simplest. Files and regularly data polling. – ioannis Jun 18 '12 at 08:32
  • @ioannis which is why I suggested ape ape is a server already built you just have to tell it a few things ... very little though – gabeio Jun 18 '12 at 08:34
  • @gabeDel great idea but in this case it might not be the best way of doing things. He could be running the server easily on a Raspberry Pi, which is limited on resources. Not a good idea to run another process just for polling 5-10 variables. Let the client do the heavy lifting- pull every now and then. ;) (thanks for bringing ape ape to my atttention btw) – ioannis Jun 18 '12 at 08:40
  • true but it depends on what he is truly trying to accomplish which has not been state clearly though if you are right and this will be on raspberry pi the better idea is indeed polling – gabeio Jun 18 '12 at 08:42
  • Everything will be on the raspi indeed. The idea is that clients access it using mainly mobile devices. – Tomas Jun 18 '12 at 12:15
  • ah well mobile devices are not best with push request because it keeps an open connection and it will kill mobile devices very fast an example of an implementation of push on mobile devices would be [kik](kik.com) and it kills mobile devices at about 10% per hour. – gabeio Jun 18 '12 at 17:07