PB daemon to process AJAX calls

Just starting out? Need help? Post your questions and find answers here.
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

PB daemon to process AJAX calls

Post by USCode »

Has anyone used PB to write a daemon to respond to AJAX calls from a web app? Seems like PB might offer superior performance to other alternatives such as Apache/PHP.

Wouldn't the PB Network Server example basically give me what I need as far as interfacing with the browser, and I just then need to add the processing code to respond to the request?
How would I parse the request data sent by the AJAX call?
How would I then correctly format and send that data BACK to the browser in response?
Can I use PB to send a response back to an asynchronous AJAX request? Or would only synchronous AJAX calls be easily responded to?
Also, any thoughts on making this scalable would be great.

Any high-level advice on how to accomplish all this is appreciated!
Thanks!
DarkPlayer
Enthusiast
Enthusiast
Posts: 107
Joined: Thu May 06, 2010 11:36 pm

Re: PB daemon to process AJAX calls

Post by DarkPlayer »

Hi,

I would not recommed you to write your own HTTP server as it's much more complicated than you might think in the first moment. The HTTP 1.1 Protocol does support many features like chunked data transfers, range requests, If Modified Since requests and many more things. You don't need to implement all of these things, but if you wan't to improve the performance, you may also want to include the network performance enhancements of HTTP. Some Webservers and Webbrowsers also implement the SPDY protocol of Google to get a better performance. Even if you only implement the basic parts, you still need to parse URLs and this is also very complicated. There are much more allowed URLs then you might think. For exmaple http://2915181464 is also a valid URL. You need to able to parse URLs like http://example.com:80/a/b/../../e%20?a=50&t=4%20%20#cd which requires a lot work to create a RFC comform parser.

If you realy want to answer HTTP requets via PureBasic, you should create a cgi (or better fast-cgi) module. Almost all Webservers like Apache, lighttpd or nginx support this. A (fast) cgi module is just a process which gets the request as input and all output will be sent via network to the client. You don't need to care about all the network stuff like HTTP. If you use a normal cgi module, you can access things like query strings or the remote ip address as preparsed enviroment variable. This is a better approach than writing a whole HTTP Server.

You should also find some cgi examples in the forum.

DarkPlayer
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: PB daemon to process AJAX calls

Post by USCode »

Thanks for the feedback DarkPlayer
jamirokwai
Enthusiast
Enthusiast
Posts: 796
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: PB daemon to process AJAX calls

Post by jamirokwai »

Hi USCode,

have a look at the Atomic Webserver: http://www.development-lounge.de/viewtopic.php?t=715
I mangled around with this, and changed "GET" to "POST". This way, you are able to also get the post-commands from browsers to catch AJAX-commands.

During development of my Server-Client-Game, I experienced some serious problems with things like streaming, which is a lot of work.
I spend about 2 weeks making good progress, but stopped because I *needed* streaming. Lastly, I got back to PHP/mySQL with AJAX. Works better now...

If you need to add AJAX to your own projects for only transmitting simple texts, Atomic Server may perfectly fit. Otherwise, do not start :-O
Regards,
JamiroKwai
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: PB daemon to process AJAX calls

Post by USCode »

Yes, the data that will be exchanged back and forth is very narrowly defined, so the flexibility necessary on the server side is minimal.
However, I've reached similar conclusions that you have - who knows what our future needs will be.
Thanks JamiroKwai
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: PB daemon to process AJAX calls

Post by Num3 »

I haven't made a deamon but a cgi!

It works flawless with php and ajax on a windows or linux webserver.

It's a secure backend that encripts/decripts data from a mysql database and makes the login and user auth part.

Just search the forums for the cgi code, when i get to the server i will post a snippet of php/ajax code so you can understand how it works (it will save you a bunch of time ;) )
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: PB daemon to process AJAX calls

Post by Num3 »

Here y'a go!

Magic PHP code

Code: Select all

<?php

	// REQUIRES CURL extension
	
	header('Content-Type: text/html; charset=ISO-8859-1'); //IMPORTANT if you're using Latin characters !
	
	function mycgi_post($post = null)
	{
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
		$domainName = $_SERVER['HTTP_HOST'].'/';
		$crl = curl_init();
		$timeout = 5;
		$url = $protocol.$domainName."cgi-bin/mycgi.exe"; // YOU'RE CGI HERE
		curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
		if (isset($post))
		{
			curl_setopt ($crl, CURLOPT_POST, 1);
			curl_setopt ($crl, CURLOPT_POSTFIELDS, $post);
		}
		curl_setopt ($crl, CURLOPT_URL,$url);
		$ret = curl_exec($crl);
		curl_close($crl);
		return $ret;
	};
	
	echo mycgi_post('proceduretocall=echoname&user=myname');
	
?>
	
Or call the cgi directly from a form ...

Code: Select all

<form id="login" name="login" action="cgi-bin/mycgi.exe" method="post" accept-charset="ISO-8859-1" enctype = "application/x-www-form-urlencoded">
....
</form>
Or call it using ajax (jQuery used here)

Code: Select all

var username = $("#inputbox").val(); //Get the username value from some inputbox
$.post("cgi-bin/mycgi.exe", {proceduretocall:"echoname",user:username},
function(data) {
	$("#yourreturndiv").html(data);
});
Purebasic PseudoCode, uses 'CGI using native PB' include here from the forums
http://www.purebasic.fr/english/viewtop ... 12&t=35321

Code: Select all

XIncludeFile "cgi.pbi"
CGI_Init() ; I put all cgi start up code in this procedure DYI 

ForEach CGIFormVars()
  
  ;-Procedures
  Select CGIFormVars()\Name
      
    Case "proceduretocall"
      
      If CGIFormVars()\Value = "echoname"
        
        ForEach CGIFormVars()
          If LCase(CGIFormVars()\Name) = "user"
            user = CGIFormVars()\Value
          EndIf    
        Next
        
        HttpAnswer$ + "<div><H1>You're name is "+user+"</H1></div>"
        WriteConsoleData(@HttpAnswer$, Len(HttpAnswer$)) 
      EndIf
  EndSelect
  
Next
CGI_End() ; I put all ending code and cleanup in this procedure
End
Post Reply