Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AKhateeb opened this issue Aug 9, 2016 · 23 comments
Open

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

AKhateeb opened this issue Aug 9, 2016 · 23 comments

Comments

@AKhateeb
Copy link

AKhateeb commented Aug 9, 2016

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
Copy link
Owner

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

@masoud005
Copy link

we are waiting...

@kishor10d
Copy link
Owner

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

@jibinprabha
Copy link

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

@kishor10d
Copy link
Owner

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

@RohmadEW
Copy link

RohmadEW commented Feb 7, 2018

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
Copy link

easy, just use the client id as a reference.

@abhiburk
Copy link

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

@mejuliver
Copy link

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

@abhiburk
Copy link

Can u give me am example ?

@mejuliver
Copy link

@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
Copy link

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

@mejuliver
Copy link

@abhiburk no, we're not using loop there

$clients[1]['conn']

we're just directly get it from clients array.

@abhiburk
Copy link

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

@mejuliver
Copy link

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
Copy link

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

@mejuliver
Copy link

@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
Copy link

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

@abhiburk
Copy link

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

@mejuliver
Copy link

@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
Copy link

iwebgeek commented May 9, 2019

@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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants