0

I have created application with CodeIgniter. I have view in my application. After rendering that view, is it possible to update/change only certain parts of rendered view from codeigniter backend? I would prefer to change parts of the view by rising events using codeigniter Events class. Is this possible?

This my controller home.php:

<?php namespace App\Controllers;

use CodeIgniter\Events\Events;

class Home extends BaseController
{
    public function index()
    {
        echo view('test_events');
    }

    public function testEvent(){
        Events::trigger('kukuni');
    }
    

    //--------------------------------------------------------------------

}

This is my test_events.php file:

<?php 

    use CodeIgniter\Events\Events;

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to CodeIgniter 4!</title>
    <meta name="description" content="The small framework with powerful features">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" type="image/png" href="/favicon.ico"/>

</head>
<body>

<?php 

    Events::on('kukuni', function()
    {
            echo "jjjjjjjjjjjjjjjjjjjj" ;

    });

?>

</body>
</html>

Let me clarify even more what i want to achieve: I have firm knowledge, how to make for example Ajax calls with JavaScript from client side to server and update Dom and JavaScript data according to returned data. But in this case the "initiator" of update is client. I want the initiator to be server, not the client: something happens on server side, like some event gets fired, the view should change accordingly.

In the example i provided, i tried to rise event kukuni inside testEvent() controller function and handle that event inside test_events.php which should echo some random string but browser page does not get updated with this random string. I also prefer not to use any sort of manual polling in regular intervals from client to server.

Is this kind functionality feasible with CodeIgniter? What am i missing and/or doing wrong?

Thank you

Tornike Shavishvili
  • 1,244
  • 4
  • 16
  • 35
  • 1
    You need to provide code of what you have tried and specific errors you're encountering. That said, and without any further detail from you, I can only say that yes, it can be done. There's many many ways to do that. Most of not all involve making XHR requests to the backend and update the view on the DOM using vanilla JavaScript or the JS framework of your choice. – Javier Larroulet Aug 22 '20 at 17:47
  • @JavierLarroulet I have clarified and undated my questiono – Tornike Shavishvili Aug 22 '20 at 20:06
  • You need to look at using websockets. This may be of help - https://stackoverflow.com/questions/14512182/how-to-create-websockets-server-in-php – TimBrownlaw Aug 23 '20 at 00:21
  • @TimBrownlaw I was hoping that i was doing something wrong. Does this mean, that codeigniter does not have this functionality implemented natively? Is there any framework that does this on its own? – Tornike Shavishvili Aug 23 '20 at 09:09
  • 1
    The only way a webserver can talk to/be aware of a client is by using a persistent connection which is what websockets do. No, no framework has this "built in". You add it, if you want this functionality. In the case of CI, I did find this - https://stackoverflow.com/questions/55881767/how-to-include-the-codeigniter-websocket-library-correctly which I might be looking into soon. – TimBrownlaw Aug 23 '20 at 19:47

1 Answers1

1

The only way to achieve this is using WebSocket protocol to keep an open connection between the client and the server and use it send updates/changes without client interaction (server events). Here is an implementation of Ratchet WebSocket using CodeIgniter with authentication: https://github.com/romainrg/ratchet_client

Ahmed Sheashaa
  • 132
  • 2
  • 11
  • Thanks for your answer, I managed to make ratchet work and i even managed to run websocket and apache server on same port, but how can i start ratchet on codeigniter startup? I tried to use event 'post_system', and 'pre_system', but as soon as i run $ratchehterver->run(); codeigniter stops serving requests without any error. What is the correct way starting rathceht server in codeigniter? – Tornike Shavishvili Aug 28 '20 at 10:48
  • Are you running the server from cli? Can you share your running code block? – Ahmed Sheashaa Aug 28 '20 at 10:59
  • I have added following code in Events.php: $SocketServer = null; Events::on('post_system', function () { $SocketServer = IoServer::factory( new HttpServer( new WsServer( new \ServerEvents\Chat\Chat() ) ), 6745 ); $SocketServer->run(); }); – Tornike Shavishvili Aug 28 '20 at 11:01
  • i am using XAMPP server for testing purposes and starting apache server from xampp control panel gui – Tornike Shavishvili Aug 28 '20 at 11:02
  • Obviously it runs synchronously and blocks execution of other requests, can something be done about this? – Tornike Shavishvili Aug 28 '20 at 11:15
  • I think that you are running a new server every time a page is sent to the browser. You should only run ws server once. If I am right, this only works for the first rendered page and then the server crashes. I had the same problem when I was running a new ws server continuously in the same route. – Ahmed Sheashaa Aug 28 '20 at 11:15
  • I managed to solve this problem by running the server from cli with if(php_sapi_name()==="cli") that stoped server crashes – Ahmed Sheashaa Aug 28 '20 at 11:18
  • Actually server is not crushing, request are unresponsive (browser tabs are waiting for requests from server which never arrive, and there are no errors in log). – Tornike Shavishvili Aug 28 '20 at 11:24
  • If i do not find graceful solution to this problem i am going to give up on ratchet and codeigniter and look for some other framework and web socket implementation :( – Tornike Shavishvili Aug 28 '20 at 12:49