1

I'm making connections via thrift (node-thrift) to a backend server to make api calls, but the communication is bidirectional (push/pull) to NodeJS.

While a user is browsing around different URLs, and Node is churning out jade templates and javascript files via Connect/Express routes, how do I maintain the connection to the backend server, and output (as an example) the connection status as part of the rendered jade output?

I have the connection object, but what do I do with it?

Sockets and port communication is generally a new area for me, so any help would be appreciated.

Keep in mind that backend server is not communicating to the web browser as the client, but rather the NodeJS server as the client.

qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • 1
    You could try to use an iframe for a lot of the site, and reload that while keeping the connection open on the main shell of the page? Otherwise I think this would be difficult to do without having some cookie data from request to request, and to preserve an association between some key in the cookie and some thread on the server. This seems too error prone. – Erik Christianson Sep 17 '13 at 21:42
  • The connection can be used by any user, we dont have to associate it with a particular sessionID just persist the connection object. I wonder if I'm generally just approaching this incorrectly. The iframe approach seems wonky, too, unfortunately. – qodeninja Sep 17 '13 at 21:55

1 Answers1

1

(updated after discussion in comments)

So it looks like thrift is TCP-based which means the node client is going to keep the connection to your thrift API server open. This is entirely independent of what your node/express app server is doing with the browser clients. So if you keep a reference to your thrift client available to all requests, by attaching it to the app object for example, you should be able to determine it's current status and include that information in HTTP responses to the browser. There's not going to be any automatic coordination or association of any kind between your express app server handling browser HTTP requests and your thrift client making RPC calls to the API server. If you want coordination, you need to code that explicitly, but sending an HTTP response to a browser isn't going to automatically close your thrift TCP connection to the thrift RPC server (and same story vice versa).

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • This is using Apache Thrift (http://thrift.apache.org/). I dont think Thrift uses websockets -- (RPC I think), but I dont know for sure. I know it reads and writes bytes of data to a buffer. Thanks for the bit about the connection not closing -- that does make sense though I can't wrap my head around how to implement it! – qodeninja Sep 17 '13 at 22:05
  • Well if it's just HTTP long-polling then I think just node's core http module will behave fine by default for that. – Peter Lyons Sep 17 '13 at 22:24
  • Websockets and RPC are two different things. An RPC is /what/ you want to do, and (web)sockets could be a possible way /how/ to do it. – JensG Sep 17 '13 at 22:32
  • @JensG Hi, thanks for clearing that up. How could I determine if websockets are being used? (npm node-thrift) – qodeninja Sep 17 '13 at 22:35
  • Seems not to be the case. The calls used in the Thrift library code for NodeJS are net.createServer() and net.createConnection(). – JensG Sep 17 '13 at 22:54
  • Did you take a look at http://stackoverflow.com/questions/4495749/best-approach-for-cross-platform-real-time-data-streaming-in-php ? The answer given there seems to match your problem. – JensG Sep 17 '13 at 23:00
  • Thanks, this helped me understand the problem/solution better! – qodeninja Sep 18 '13 at 17:48