How to implement node-postgres on a user's browser?

So I've tested this particular example on my local machine: http://bjorngylling.com/2011-04-13/postgres-listen-notify-with-node-js.html

It worked! So now when I update a specific table, and am running my node.js file(from the tutorial) -I get an instant notification on my Terminal(mac)!! cool!

But how do I implement this onto a client's browser??

First of all, in the node.js script you'll notice that I have to connect to the database with my username and password:

pgConnectionString = "postgres://username:pswd@localhost/db";

I obviously can't have that floating around in the .js file the user's browser downloaded.

Plus I don't even know what scripts I'd have to include in the <head>. I can't find anything anywhere on how this is used in the real world.... All I see are neat little examples you can use in your command line.

Any advice, or guidance in the right direction would be awesome! Thanks.

Answers


You can't.

Node.js runs directly on your server, speaking directly to the native libraries on that machine. I'm not sure exactly what the postgres driver you are using does, but either it speaks to the postgres libraries OR it speaks directly with sockets on the local or a remote database server.

Neither of these methods can be used directly from a browser environment (it can't speak directly to the native libraries and it can't speak "raw" sockets).

What you can do is to have the web client speak to your own server process on a server (running node.js or similar), which would then speak to the database on behalf of the client.

Assuming you also need to database server to be able to initiate notifications to the client, you would need to use a bi-directional communication module like socket.io or similar.


You can do: combine your JS running on node.js which accesses Postgres listening for events with a node.js based WebSocket server, implement PubSub and push out to HTML5 browsers .. WebSocket capable ones.


Another option: use a generic WebSocket to TCP bridge like https://github.com/kanaka/websockify and implement the Postgres client protocol in JS to run in browser. That is doable, but probably not easy / for the faint hearted.


Need Your Help

Another SQL Max/Count Qestion

sql sql-server-2008 count max

Have seen many Max/Count Qs answered but I can't get my head around this one. Say I have two tables, the first people and the second result_sets. They contain data as follows;

What is a Facebook/Twitter OAuth token internally?

oauth token twitter-oauth

Ok, I really know what is it. I work with Facebook/Twitter API long ago. And it works, there is no bugs, sorry :). However, the following thing haunts me.