Forever Frame comet technique? - real-time

Can someone please explain me how the "Forever Frame" technique works ? A code example would be awesome! I went through all the material i could find in Google but i still don`t have much of an idea how it works.

Dylan Schiemann gives a great explanation.
In short, it uses "chunked encoding", a feature of HTTP/1.1 intended for an entirely different purpose, but which allows the server to maintain a connection to the client indefinitely, sending additional data to the client at will.
As Dylan says :
This technique is very low-latency because it avoids HTTP and TCP/IP set-up and tear-down by reusing a single long-lived connection.
http://cometdaily.com/2007/11/05/the-forever-frame-technique/
The rest of his article is well worth a read.

Related

stubby4j handling SOAP request

I like to serve a default response form stubby4j for a SOAP request with the same value I am receiving in request.
post: "[\\s\\S]*param1.([^<]+)[\\s\\S]*param2.([0-9]+)[\\s\\S]*param3.([^<]+)[\\s\\S]*param4.([^<]+)[\\s\\S]*"
Now the problem is, I can't ensure the sequence of input parameters coming in the request. So there is a chance that param2 comes first.
How can we handle this?
I really dint need it. I noticed that requests coming from the same client maintains the sequence of input field. So I need not to worry to compare or capture them with different patterns.
However due to some limitations of stubby4j, I have made my own utility and switched to stubby-db now.
Hi #Amit Kumar Gupta,
Thank you for using stubby4j and thank you for your question and I am really sorry for replying late. You are welcomed to have a look at stubby4j library now, the docs are at: https://stubby4j.com, as it has undergone many changes and improvements since 2016. I hope you can find features needed, if not, please raise an issue or a feature request.
In addition to HTTP/1.1, it also supports behavior stubbing over HTTP/2 and WebSockets (over HTTP/1.1) protocols. In the near future it will support WebSockets bootstrapping over HTTP/2
Plus, stubby4j has been Dockerized with the official images stored at https://hub.docker.com/r/azagniotov/stubby4j
Thank you.

Should I connect directly to CouchDB's socket and pass HTTP requests or use node.js as a proxy?

First, here's my original question that spawned all of this.
I'm using Appcelerator Titanium to develop an iPhone app (eventually Android too). I'm connecting to CouchDB's port directly by using Titanium's Titanium.Network.TCPSocket object. I believe it utilizes the Apple SDK's CFSocket/NSStream class.
Once connected, I simply write:
'GET /mydb/_changes?filter=app/myfilter&feed=continuous&gameid=4&heartbeat=30000 HTTP/1.1\r\n\r\n'
directly to the socket. It keeps it open "forever" and returns JSON data whenever the db is updated and matches the filter and change request. Cool.
I'm wondering, is it ok to connect directly to CouchDB's socket like this, or would I be better off opening the socket to node.js instead, and maybe using this CouchDB node.js module to handle the CouchDB proxy through node.js?
My main concern is performance. I just don't have enough experience with CouchDB to know if hitting its socket and passing faux HTTP requests directly is good practice or not. Looking for experience and opinions on any ramifications or alternate suggestions.
It's me again. :-)
CouchDB inherits super concurrency handling from Erlang, the language it was written in. Erlang uses lightweight processes and message passing between those processes to achieve excellent performance under high concurrent load. It will take advantage of all cpu cores, too.
Nodejs runs a single process and basically only does one thing at a time within that process. Its event-based, non-blocking IO approach does allow it to multitask while it waits for chunks of IO but it still only does one thing at a time.
Both should easily handle tens of thousands of connections, but I would expect CouchDB to handle concurrency better (and with less effort on your part) than Node. And keep in mind that Node adds some latency if you put it in front of CouchDB. That may only be noticeable if you have them on different machines, though.
Writing directly to Couch via TCPSocket is a-ok as long as your write a well-formed HTTP request that follows the spec. (You're not passing a faux request...that's a real HTTP request you're sending just like any other.)
Note: HTTP 1.1 does require you to include a Host header in the request, so you'll need to correct your code to reflect that OR just use HTTP 1.0 which doesn't require it to keep things simple. (I'm curious why you're not using Titanium.Network.HTTPClient. Does it only give you the request body after the request finishes or something?)
Anyway, CouchDB can totally handle direct connections and--unless you put a lot of effort into your Node proxy--it's probably going to give users a better experience when you have 100k of them playing the game at once.
EDIT: If you use Node write an actual HTTP proxy. That will run a lot faster than using the module you provided and be simpler to implement. (Rather than defining your own API that then makes requests to Couch you can just pass certain requests on to CouchDB and block others, say, for security reasons.
Also take a look at how "multinode" works:
http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/

How to maintain a persistant network-connection between two applications over a network?

I was recently approached by my management with an interesting problem - where I am pretty sure I am telling my bosses the correct information but I really want to make sure I am telling them the correct stuff.
I am being asked to develop some software that has this function:
An application at one location is constantly processing real-time data every second and only generates data if the underlying data has changed in any way.
On the event that the data has changed send the results to another box over a network
Maintains a persistent connection between the both machines, altering the remote box if for some reason the network connection went down
From what I understand, I imagine that I need to do some reading on doing some sort of TCP/IP socket-level stuff. That way if the connection is dropped the remote location will be aware that the data it has received may be stale.
However management seems to be very convinced that this can be accomplished using SOAP. I was under the impression that SOAP is more or less a way for a client to initiate a procedure from a server and get some results via the HTTP protocol. Am I wrong in assuming this? I haven't been able to find much information on how SOAP might be able to solve a problem like this.
I feel like a lot of people around my office are using SOAP as a buzzword and that has generated a bit of confusion over what SOAP actually is - and is capable of.
Any thoughts on how to accomplish this task would be appreciated!
I think SOAP is the wrong tool. SOAP is a spec for exchanging structured data. For your problem, the simplest thing would be to write a program to just transfer data and figure out if the other end is alive. Sockets are a good way to go. There are lots of socket programming tutorials on the net. Pick your language, and ask Mr. Google. Write a couple of demo programs to teach yourself how it works. Ask if you have more specific questions.
For the problem, you'll need a sender and a receiver. The sender sends data when it gets it, the receiver waits for data and hands it off when it arrives. Get that working first. Next, add in heartbeats; a message that says "I'm alive", sent periodically. Get that working next. You'll need to be determine the exact behavior you want -- should both sides send heartbeats to the other end, the maximum time you are willing to wait for a heartbeat, and what action you take should heartbeats stop arriving. The network connection can drop, the other end can crash, the other end can hang, and perhaps there are other conditions you should think about (e.g., what if the real time data is nonsense?). Figure out how to handle each condition, and code up the error handling. Test it out, and serve with a side of documentation.
SOAP certainly won't tell you when the data source goes down, though you could use "heartbeats" to add that.
Probably you are right and they are just repeating a buzz word, and don't actually know much about what SOAP is or does or have any real argument for why it ought to be used here.

PostgreSQL socket connection

Is it possible to send data on a socket over a TCP?IP connection from a PostgreSQL stored procedure or trigger?
If you know of any useful examples pleas tell me.
If you know of something similar for other data base systems it would also be helpful.
Thanks in advance.
Answer from Milen solves the technical side of the question. But there is also a problem of interactions.
Let's assume you have trigger that does things over TCPIP. This means that the query that launched the trigger can take a long time (think network issues, service issues, firewalls).
Usually much better solution is to store information in some kind of queue, and addition of service, which checks the queue (perhaps using NOTIFY/LISTEN feature of PostgreSQL), and does what's necessary over TCP/IP - with proper handling of long connections, retries and so on.
If you'd be inclined to use such mechanism, you might want to check PgQ from SkyTools.
Yes, it's possible but you have to use one of the "untrusted" languages - PL/perlU, PL/pythonU, etc.
And then all examples you need you'll find in the documentation of the respective language.

iPhone socket program

I want to create an extremely simple iPhone program that will open a telnet session on a lan-connected device and send a sequence of keystrokes. Most of the code I've seen for sockets is overwhelming and vast overkill for what I want to do:
open telnet socket to IP address
send ascii keystrokes
Any simple code examples out there I can play with?
I'd suggest you check out the Asyncsocket project:
It makes socket programming really easy; no messing with threads yourself and things happen asynchronously without much fuss.
I think there is a sample project with a client/server to get you started.
Do yourself a favor: go read at least first 6 chapters of this Steven's book in which you can find plenty of simple examples and many advices how to avoid common pitfalls with network programming. Without doing that you will end with a buggy, slow and incomplete client.
Go through this you will have to get basic idea of socket programming
http://ichuiphonedev.blogspot.in/2012/07/a-basic-idea-of-socket-programming-in.html
i cant repost my code here, because SO considers it spamming to repeat your answer.
check out this sample code and tutorial link. works like a charm and is really simple to implement, less than 3 minutes and you are up and going (IF you have a socket server ready).