3

I'm just investigating OPA and trying to make the leap from a traditional LAMP background, so here's my first of many newbie questions:

Can I have two OPA apps sharing the same database, say one which writes into a database and another that reads from it?

russellfeeed
  • 617
  • 8
  • 10

1 Answers1

5

Yes, it's certainly possible. A simple but complete example:

[db.opa]

database ./counter
db /counter : int
Counter = {{
  read() = /counter
  inc() = /counter <- read() + 1
}}

[db_read.opa]

server = one_page_server("Counter app", -> <>Counter value: {Counter.read()}</>)

[db_write.opa]

_ = Scheduler.timer(1000, -> Counter.inc())

Compile with:

 opa db_read.opa db.opa -o db_read.exe
 opa db_write.opa db.opa -o db_write.exe

Run the database server for database counter on port 5001:

 opa-db-server -b 127.0.0.1:5001 --db-local counter

Run the applications, connecting to this database:

 ./db_read.exe --db-remote 127.0.0.1:5001
 ./db_write.exe --db-remote 127.0.0.1:5001

The db_write app updates the counter every second. You can see that with the db_read app by visiting localhost:8080 (and refreshing the page).

Hope the Opa-DB experts will correct me if I got something wrong.

akoprowski
  • 3,297
  • 1
  • 18
  • 26
  • Thanks akoprowski, I'll try it later but it certainly looks like the key is using `opa-db-server -b 127.0.0.1:5001 --db-local counter` – russellfeeed Jul 27 '11 at 15:00
  • Yep, just tried it and it worked. Got a few warnings about "Can't set node properties on client" but the concept worked fine. Thanks! – russellfeeed Jul 27 '11 at 20:02