Code Monkey home page Code Monkey logo

pjstreamer's Introduction

php to javascript streamer

Library for streaming output from php to javascript via ajax in realtime.

Usage

Contains two parts of interaction

PHP class

Output form the php files that to be streamed over to should be passed through the PJStreamer class.

  1. Include the php class : pjstreamer.php
  2. Initiate the stream by calling PJStreamer::start() method
  3. Send output over the stream using PJStreamer::send($output)
<?php
include './pjstreamer.php';

PJStreamer::startStream();

for($i=1;$i<5;$i++){
    PJStreamer::sendStream($i);
    sleep(1);
}
?>

What happens?

Above library just do realtime php output, that's it flushes the existing buffer so any output from the php script will immediately be displayed

Javascrip class

Output form the streaming php script is captured by an XMLHTTPObject and is processed by the JS class.

  1. Attach pjstreamer.js
  2. Make a new XMLHttpObject with the GET/POST parameters as for your requirement
  3. Initiate a new PJStreamer class with your XMLHTTPObject and a callback function as parameters
  4. Start the process by calling PJSreamer.start() method
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>PJStreamer demo</title>
        <script type="text/javascript" src="pjstreamer.js"></script>
        <script>                      
            window.onload = function(){
                var xmlhttp;
                if (window.XMLHttpRequest)
                  {// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
                  }
                else
                  {// code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                  }
                
                xmlhttp.open("GET","stream.php",true);

                new PJStreamer(xmlhttp,function(data){
                    alert(data);
                }).start();
            }
        </script>
    </head>
    <body>
         
    </body>
</html>

What happends?

Above library can remove the unwanted browser tweaks sending from the PJStreamer php script, and deliver ONLY the new data.

NOTE: Jqyery AJAX wont work since their callback is made only after status=200 is received. But for streaming you need to check for response in all.

Output

Aobe code togather will result in a 4 textbox displaying 1-4, each displayed with an interval of 1 second

Confused?

Feel free to contact me on

@email : [email protected]

@twitter : @_manzzup_

@blog : http://manzzup.blogspot.com

pjstreamer's People

Contributors

manzzup avatar

Watchers

James Cloos avatar Patrick Laxton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.