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

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.

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.

Related

How can I detect what program is listening to a TCP/IP port in Windows?

I have an application that I inherited that listens on port 7001 for UDP broadcasts from our in-house test equipment, and I recently updated another application that needs to do the same thing. Both applications must be able to coexist on the same computer.
Currently, when my recently updated application attempts to bind to the port to listen for UDP broadcasts and fails, it simply reports that the port is not available and suggests that the inherited application is probably running. How can I get my application to detect what application is actually listening on that port? I've done a Google search and have even searched this site, but so far I have been unable to find anything except to use Task Manager, TCPView, or netstat at the command line.
I would prefer a technique that either uses the Windows API or a Windows system COM component, since both applications are written in Visual Basic 6.0. (I know, I know, but I must maintain these applications since they are mission critical.) However, a .NET solution would would also be useful in case I need it in my new development efforts.
Use:
netstat -n -o
That will show the process ID and from there you can either look in the Task Manager's process viewer, go to menu View → Columns... and check the Process ID (PID). Then you can see the name of the process listening on that port.
Of course, you're wanting a programmatic way of accomplishing this and the GetTCPTable2 API is best as was already suggested. In fact, if you look at the IAT (Import Address Table) for netstat.exe, it actually uses that API to get that information.
There is a way to communicate directly with a command window and get its output using pipes and it would work fine, but the ideal way is to simply use the same API netstat uses.

Spawning child tcp server in meteor

I want to create a Meteor application that interacts with the user and at the same time receives market data from multiple TCP sockets. I intend to channel the market data (from various TCP sockets) into one collection.
I had been looking around but i do not see a net.createServer method for Meteor. Can anyone give any pointers for me to start?
Thanks in advance!
Meteor has a packaging system that allows you to use node modules. So I would look at node modules to find the server functionality you want to add. The node core module http might be a good start.
You can also just search the NPM registry for a module with functionality that fits your needs better. Then just include it in you Meteor app through the packaging system. I have included the NPM pubsubhubbub in an rss aggregator that listens for pings and fills client collections so the package I used might be an example to follow.
Listening for data arriving on other ports or other domains will not work for a deployment on meteor.com so I think what you are describing will also require you to host the app.

Bluetooth: how to create a socket for connecting to a service with specific UUID with Bluez?

I am working with the BlueZ libraries for managing the bluetooth stack under linux. I am trying to open a socket that should connect with a specific service whose UUID is known. I have successfully tried to open sockets between a server and a client following the examples here:
http://people.csail.mit.edu/albert/bluez-intro/c404.html
where it is also described how to explore services in a remote host; however, what I cannot figure out is how to specify the UUID while connecting the socket. This is something quite straightforward on other contexts, as it happens when using Android libraries; however, with BlueZ I haven't found examples on the web.
Thanks for the help!
Stefano
-- added some details in a reply... don't know if it's right or not; if not, sorry
thanks for your help!
In my post I said I wanted to connect a socket with a specified UUID since I had in mind a function like createRfcommSocketToServiceRecord as can be found in:
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createRfcommSocketToServiceRecord%28java.util.UUID%29
which returns a socket given a certain UUID. I am developing two softwares, one being the client and the other the server, therefore it's a service I am creating, not a standard one. I publish such service on the server with a specified UUID, and I would like to connect to it from the client. The server is running fine, since with an android platform, exploiting the mentioned createRfcommSocketToServiceRecord method, I am able to connect to the server with the right service.
The example you posted is very clear as well, but unfortunately it is in Python, while (I forgot to point out) I am using Bluez as a C library. I am able to exploit the SDP and inspect services on a remote server; however, when it is the time of setting up the socket, I don't see how to specify the port. I thought about the rc_channel, but as far as I understood it is the bluetooth channel (ranging from 1 to 11 or something similar). Could you please point out Where I should be able to specify the port while creating the socket in the client? And where am I able to find the right port in the complex structure the sdp is dealing with? -- referred to:
http://people.csail.mit.edu/albert/bluez-intro/x604.html
Thanks again for the help!
You don't connect sockets by UUID. You use a socket to connect to remote device and browse its SDP to discover the UUIDs it offers, and map that to a socket port. Here's an example of this process using PyBluez, python wrappers above BlueZ
http://people.csail.mit.edu/albert/bluez-intro/x290.html

How to connect to remote mysql from iphone application?

i am newbie to iphone programming. I am working on an iphone project.
My question is:
How to connect my iphone application to a remote server mysql database directly?
There are many ways to connect through PHP scrip/ json/xml/ webservices.
But i dont need that. I would like to have direct connetion to mysql of remote server without any intermediate?
Basically you would need to build a mysql client lib for iOS and then use that from Objective-C. This blog post should get you started. As stated by peko you should really reconsider that you are doing the right thing. The mysql network protocol isn't exactly made for mobile clients where connections can drop, be unavailable, etc.
You shouldn't do this because someone could sniff the traffic and get the username and password for your database, or inject sql-code.
Here's a MySql objective-C client for iPhone. It was traditionally available only for OSX but they just released it for iOS:
http://www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/
I haven't used it. I would still strongly recommend exposing via services. I would also be interested in why you need to directly connect to the DB and why services isn't appropriate :)

Is there a program that can send data via sockets to a server, to test it?

I have a simple socket server, that waits for a message and responds. How can I test this? Is there a client app or something that can help me send some data on a specific port to a server, and see the output? Telnet?
Simple telnet client works well for such tests. You can also try PuTTY in either Telnet or Raw connection modes. Both allow you to choose the port you want to connect to.
Also a tool like Microsoft Network Monitor is pretty useful to analyse the protocol's data flow if you don't have direct control (by logs) on what's being sent over the wire.
netcat should be able to handle anything you need to do with socket testing.
http://pctechtips.org/netcat-the-swiss-army-knife-useful-commands/#more-103
Windump - tcpdump works well, and has binaries available to Windows
You have several choices:
1) You can use Chrome or FireFox plugins such as:
Simple WebSocket Client
Smart WebSocket Client
2) You can use WebSocket or ReconnectingWebSocket in JavaScript. this is a simple test with WebSocket:
Open Chrome browser
Press F12 or Right Click on page then click Inspect Element to open developer console
You shoud see a window with several tabs. Go to console tab then write this code.
ws = new WebSocket('ws://<YOUR-DOMAIN>:<PORT>')
ws.send('STH YOU WANT TO SEND')
3) You can use websocket-client. It is a simple python library. You must install python and pip, then websocket-client via pip. This is a simple example:
Go to python shell and type.
from websocket import WebSocket
ws = WebSocket()
ws.connect('ws://<YOUR-DOMAIN>:<PORT>')
ws.send('STH YOU WANT TO SEND')
hit the command line, and just use telnet.
if you have a simple echo server, go to town.