Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
1  
Not sure there's any "technically" about it. You just can't (and shouldn't even if you could). – Richard Huxton Jun 15 '12 at 7:46
    
I've come to understand nodejs much much better since I asked this question and I wanted to thank you for taking time to give a kind answer to a rather silly question! – Joe B Mar 25 '13 at 17:16
    
you could use litesocket / server-side events instead of socket.io: github.com/williamwicks/litesocket – DTrejo Dec 4 '13 at 0:32

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.