Welcome Guest, Not a member yet? Register   Sign In
How to implement EventSource on Codeignigter app?
#1

(This post was last modified: 02-22-2020, 05:15 PM by SmokeyCharizard.)

I'm trying to send my end user simple updates on the progress of a data conversion in my app. I researched for a while and found EventSource to be the best solution (lightweight and simple). The problem is, I can't seem to get it to listen for events on the correct model/view/controller in codeigniter.
I have a query submission being sent from a form_open_multipart in an index.php in Views.
Code:
<?php echo form_open_multipart('c_controller/classicFirearmByDate'); ?>
<div class="form-group">
     <label>Date From -></label>
     <input type="text" class="form-control" name="date" placeholder="2019-10-15 06:46:30">
</div>
<div class="form-group">
     <label>Date To <-</label>
     <input type="text" class="form-control" name="date-to" placeholder="2019-10-16 06:46:30">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
    <div class="progress progress-striped active">
    <div class="progress-bar" id="progress" style="width:0%"></div>
<div id="message"></div>
<script>
    var element = document.getElementById('message').innerHTML;
    var source = new EventSource("<?php echo site_url('c_controller/classicFirearmByDate');?>");

    source.addEventListener("message", function(e) {
      document.getElementById("progress").innerHTML = "MESSAGE";
      console.log(e);
    }, false);

    source.addEventListener("open", function(e) {
      document.getElementById("progress").innerHTML = "OPENED";
      console.log(e.data);
      element = 'TEst';
    }, false);

    source.addEventListener("error", function(e) {
      document.getElementById("progress").innerHTML = e.readyState;
      if (e.readyState == EventSource.CLOSED) {
        document.getElementById("progress").innerHTML = "CLOSED";
      }
    }, false);

    function showdata(Jdat) {
      var data = JSON.parse(Jdat);
      document.getElementById("result").innerHTML = data.name;
    }
</script>
I use routes.php to route the data to the correct controller (c_controller.php), specifically to a method called classicFirearmByDate().
routes.php $route['c_controller/classicFirearmByDate'] = "c_controller/classicFirearmByDate";
c_controller.php
Code:
<?php
    class C_controller extends CI_Controller{
        public function classicFirearmByDate() {
            $date = trim(htmlspecialchars($this->input->post('date')));
            $dateTo = trim(htmlspecialchars($this->input->post('date-to')));

            $data['body'] = $this->classic_firearms->get_firearm_by_date($date, $dateTo);

            $this->load->view('templates/header');
            $this->load->view('firearms/firearm', $data);
            $this->load->view('templates/footer');
    }
This method validates and then passes the query to the main method of a model (cfa_model.php) called getProductByDate().
Code:
    public function getProductByDate($date, $dateTo) {
         $filter = array($date, $dateTo);
         $data = $this->proxy->catalogProductList($this->sessionId, $filter);
         ...
         //code to process $data
    }
GetProductsByDate() returns the data after being processed and getProduct (in the controller) loads a new "job finished" view with the resulting data.
I want the index.php file (the first one I mentioned) to get the updates of the processing in getProductsByDate().
I tried to set up EventSource listening in index.php to listen to cfa_model.php, but it didn't work. I was able to get it to listen to c_controller.php, but this will not suffice. I need to get consistent updates from the process itself, and the process is only in cfa_model.php.
Is this just a problem with my routes? Could it be my headers? Can I set up cfa_model.php to update another controller and hook a listener to that?
Reply


Messages In This Thread
How to implement EventSource on Codeignigter app? - by SmokeyCharizard - 02-22-2020, 03:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB