• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Websockets in codeigniter?

#1
Hey guys, it's me again.

I still have the notification issue, but I asked my professors, and they said the best solution is indeed Websockets. I found a great library for websockets in codeigniter here, and it does work, but I can't seem to figure out how to send messages from the modal to a view. I did the simple setup with the "php vendor/takielias/codeigniter-websocket/install.php --app_path=application" command, and I was able to have to 'clients' talk to one another. I was even about to get one of them to talk to my view php file.

Now I just need to get the modal to talk to the view. I tried adding this to the top of the modal file:
Code:
defined('BASEPATH') OR exit('No direct script access allowed');
require('vendor/autoload.php');

use WebSocket\Client;

And then I try to log in in the modal's constructor with this:

Code:
                $this->client = new Client('ws://0.0.0.0:8282');

                $this->client->send(json_encode(array('user_id' => 1, 'message' => null)));
                $this->client->send(json_encode(array('user_id' => 1, 'message' => 'Super cool message to myself!')));

The readme states that the first message is necessary for validation of the connection.

Despite this, I never receive the message on the view file. The socket code in my view is: 
Code:
var conn = new WebSocket('ws://localhost:8282');
    var client = {
        user_id: 1,
        recipient_id: null,
        type: 'socket',
        token: null,
        message: null
    };

    conn.onopen = function (e) {
        conn.send(JSON.stringify(client));
        $('#messages').append('<font color="green">Successfully connected as user ' + client.user_id + '</font><br>');
    };

    conn.onmessage = function (e) {
        var data = JSON.parse(e.data);
        if (data.message) {
            $('#messages').append(data.user_id + ' : ' + data.message + '<br>');
        }
        if (data.type === 'token') {
            $('#token').html('JWT Token : ' + data.token);
        }
    };

Let me guess, its the ports? I played around with them to no avail. Any ideas?
Reply

#2
(02-28-2020, 07:28 PM)SmokeyCharizard Wrote: Hey guys, it's me again.

I still have the notification issue, but I asked my professors, and they said the best solution is indeed Websockets. I found a great library for websockets in codeigniter here, and it does work, but I can't seem to figure out how to send messages from the modal to a view. I did the simple setup with the "php vendor/takielias/codeigniter-websocket/install.php --app_path=application" command, and I was able to have to 'clients' talk to one another. I was even about to get one of them to talk to my view php file.

Now I just need to get the modal to talk to the view. I tried adding this to the top of the modal file:
Code:
defined('BASEPATH') OR exit('No direct script access allowed');
require('vendor/autoload.php');

use WebSocket\Client;

And then I try to log in in the modal's constructor with this:

Code:
                $this->client = new Client('ws://0.0.0.0:8282');

                $this->client->send(json_encode(array('user_id' => 1, 'message' => null)));
                $this->client->send(json_encode(array('user_id' => 1, 'message' => 'Super cool message to myself!')));

The readme states that the first message is necessary for validation of the connection.

Despite this, I never receive the message on the view file. The socket code in my view is: 
Code:
var conn = new WebSocket('ws://localhost:8282');
    var client = {
        user_id: 1,
        recipient_id: null,
        type: 'socket',
        token: null,
        message: null
    };

    conn.onopen = function (e) {
        conn.send(JSON.stringify(client));
        $('#messages').append('<font color="green">Successfully connected as user ' + client.user_id + '</font><br>');
    };

    conn.onmessage = function (e) {
        var data = JSON.parse(e.data);
        if (data.message) {
            $('#messages').append(data.user_id + ' : ' + data.message + '<br>');
        }
        if (data.type === 'token') {
            $('#token').html('JWT Token : ' + data.token);
        }
    };

Let me guess, its the ports? I played around with them to no avail. Any ideas?
Turns out, you can send the messages from the CONTROLLER, but not the model! I made the apropriot changes to my code, and it works! Just one problem, though...

It isn't working on my AWS instance. Is there some reason why? Permissions, maybe?
Reply

#3
Thumbs Up 
(02-28-2020, 07:28 PM)SmokeyCharizard Wrote: It isn't working on my AWS instance. Is there some reason why? Permissions, maybe?

Hi, There is no issue with AWS instance. Make sure the right port permission.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.