kishor10d / CodeIgniter-Ratchet-Websocket

This library contains the demo of commenting/posting realtime using CodeIgniter+AngularJS+Ratchet PHP Websocket
93 stars 67 forks source link

How to use this project to serve the specific-user chatting #3

Open AKhateeb opened 8 years ago

AKhateeb commented 8 years ago

I have a website that should serve a student communication in our university, I found that this project is like a chat room, How can I convert it to another form where student can chat with a specific user using their ID ?? I so grateful for every help

kishor10d commented 8 years ago

@AKhateeb : Hello, we are working on this. Once we done with it, we will commit the required code. Thanks for using the library.

masoud005 commented 7 years ago

we are waiting...

kishor10d commented 7 years ago

@masoud005 : Sorry, you wait a long. Actually I stopped working on this project currently.

jibinprabha commented 7 years ago

can you please share me the code for send individual notification?

kishor10d commented 7 years ago

@jibinprabha : Sorry, you wait a long. Actually I stopped working on this project currently.

RohmadEW commented 6 years ago

I try make solution for this issue.

` class Chat implements MessageComponentInterface {

protected $repository;
private $users;
private $usersDb;

/**
 * Default constructor of the class
 */
public function __construct() {
    $this->repository = new ChatRepository;
    $this->users = [];
    $this->usersDb = [];
}

/**
 * This function is used to add the connected machine to queue
 * @param {object} $conn : Connection interface object
 */
public function onOpen(ConnectionInterface $conn) {
    $this->repository->addClient($conn);
    $this->users[$conn->resourceId] = $conn;
}

public function onClose(ConnectionInterface $conn) {
    $this->repository->removeClient($conn);
    unset($this->users[$conn->resourceId]);
    unset($this->usersDb[$conn->resourceId]);
}

public function onError(ConnectionInterface $conn, \Exception $e) {
    echo "The following error occured : " . $e->getMessage();
    $client = $this->repository->getClientByConnection($conn);
    if ($client !== null) {
        $client->getConnection()->close();
        $this->repository->removeClient($conn);
    }
}

public function onMessage(ConnectionInterface $conn, $msg) {
    $data = $this->parseMessage($msg);
    $currClient = $this->repository->getClientByConnection($conn);

    switch ($data->command) {
        case 'reg':
            $this->usersDb[$conn->resourceId] = $data->id;
            break;

        case 'msg':
            if (isset($this->usersDb[$conn->resourceId])) {
                foreach ($this->usersDb as $id => $idClient) {
                    if ($idClient == $data->target || $idClient == $data->from) {
                        $this->users[$id]->send(json_encode([
                            "msg" => $data->msg
                        ]));
                    }
                }
            }
            break;
    }
}

private function parseMessage($msg) {
    return json_decode($msg);
}

}`

`sendMsg: function (from, target, message) {

        this.socket.send(JSON.stringify({
            msg: message,
            target: target,
            from: from,
            command: 'msg'
        }));
    },`

connectionOpen: function (evt) { this.open = true; this.regisId(); this.addSystemMessage("Connected"); },

regisId: function () { this.socket.send(JSON.stringify({ id: ID_USER, command: 'reg' })); }

mejuliver commented 6 years ago

easy, just use the client id as a reference.

abhiburk commented 6 years ago

How do I send message to specific client ? please work on this project to add this functionality, Thanks in Advance

mejuliver commented 6 years ago

@abhiburk you just need to send it to the specific socket id

abhiburk commented 6 years ago

Can u give me am example ?

mejuliver commented 6 years ago

@abhiburk basically I used an array to store client resource ids. Assume we store all connected clients to an array '$clients' and this is the structure of the clients array

[ 'id' = 1, 'con' => <resource id, when client connected, store clients resources here $conn > ]

and then I just do

$this->clients[1]['con']->send( message here... );

abhiburk commented 6 years ago

But what if their are so many users like 10000 say will it iterate through all 10000 for 1 client

mejuliver commented 6 years ago

@abhiburk no, we're not using loop there

$clients[1]['conn']

we're just directly get it from clients array.

abhiburk commented 6 years ago

And one thing i am confuse with is how does client id gets store ?

mejuliver commented 6 years ago

When client is connected.

` protected $clients;

public function __construct() { $this->clients = array(); } public function onOpen(ConnectionInterface $conn) { // store client resource id $this->clients[$conn->resourceId] = array( 'con' => $conn); } `

abhiburk commented 6 years ago

So every client connecting will have diffeent resource id ? And how will i compare my db userid and resource id

mejuliver commented 6 years ago

@abhiburk absolutely, every client has their own respective resource id. I can suggest a flow for what you need, refer below...

  1. first, client connect to your websocket server
  2. server then store his/her resource id, then server send message to the client that he/she were already connected
  3. client received the message that he/she is already connected then he/she then retrieve his/her extra data from database e.g. user id, first name, last name, age etc... then client will send it over the websocket server via websocket message function
  4. server receive the message, then he will then store/add it to the clients array along with clients resource id
abhiburk commented 6 years ago

Yeah thats what i wanted to know thanks for your time and help

abhiburk commented 6 years ago

cud u give me your fb id in case if i am stuck bcz talking here is long process.thanks

mejuliver commented 6 years ago

@abhiburk you can post your problems and if you want help, you can get answers from stackoverflow.com I'm one of the folks there.

iwebgeek commented 5 years ago

@mejuliver @kishor10d How can we use database within this Chat class ? Or basically if we want the information sent to socket server to be stored into database , how can we achieve it ? Thanks in advanc.

mejuliver commented 5 years ago

You can use the onmessage function if you want to store the messages to database. i recommend using document type database like mongodb, firebase, rethinkdb

RohmadEW commented 2 years ago

I don't use this anymore. I'm using laravel + vue + redis