PHP multi user multi conversations messaging system, just like facebook :)
I decided to create this package because of the lack of real good messaging systems in php with the proper features like, deleting archiving messages for each specific user, multipile users in conversations and more. I am trying to build this in a very generic way so it could be easilly implemented for each framework or project. Currently I'll try to build with laravel and eloquent as this is the project I'm currently building.
- Message status for each user: deleted, read, unread, archived
- multipile conversations for each user
- each conversation has 2 or more users
- ability to have 1 unique conversation for each 2 users
- Get all users conversations with last message in conversation
- get all conversations messages for a specific user
users: id
messages: id, sender_id, conv_id, content, updated_at, created_at
messages_status: id, user_id, msg_id, self, status
conversations: id, updated_at, created_at
conv_users: conv_id, user_id
simply hold the users that use the messages
contains the message itself, who sent it, to which conversation, the content and the dates.
this is the "magic" it has a row for each user that got a message in a specific conversation. IE if I send a message to conversation with 4 users, we will have 5 rows in this table, the sender and the other 4 recipents, it will contains the specific user message status, as is it deleted, read, unread...
currently only has an id, and dates
holds the users in conversation relationships
- Add this: '"tzookb/tbmsg": "1.*"' to your composer.json file
- run: "composer update tzookb/tbmsg"
- Now add the service provider to your app.php file: "'Tzookb\TBMsg\TBMsgServiceProvider'"
- It would be best if you register the Facade in your app.php file: "'TBMsg' => 'Tzookb\TBMsg\Facade\TBMsg'"
- run the migrations: "php artisan migrate --package=tzookb/tbmsg"
$convs = TBMsg::getUserConversations($user_id);
This will return you a "Illuminate\Support\Collection" of "Tzookb\TBMsg\Entities\Conversation" objects. And foreach Conversation there, you will have the last message of the conversation, and the participants of the conversation. Example:
foreach ( $convs as $conv ) {
$getNumOfParticipants = $conv->getNumOfParticipants();
$participants = $conv->getAllParticipants();
/* $lastMessage Tzookb\TBMsg\Entities\Message */
$lastMessage = $conv->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
}
$conv = TBMsg::getConversationMessages($conv_id, $user_id);
This will return you a "Tzookb\TBMsg\Entities\Conversation" object. On the object you could get all messages, all participants, conv_id, and more, simply browse the object itself. Example:
foreach ( $conv->getAllMessages() as $msg ) {
$senderId = $msg->getSender();
$content = $msg->getContent();
$status = $msg->getStatus();
}
$conv = TBMsg::getConversationByTwoUsers($userA_id, $userB_id);
Simply gives you an id of the conversation between two users, this was created for redirecting to the conversation page when user tries to send a message to another user, so if there is no id returned that means that those users has no conversation yet, so we could create one.
$conv = TBMsg::addMessageToConversation($conv_id, $user_id, $content);
Simply add a message to an exiting conversation, content is the message text.
$conv = TBMsg::createConversation($users_ids=array());
Creates a new conversation with the users id's you passed in the array.
$conv = TBMsg::getUsersInConversation($conv_id);
returns an array of user id in the conversation.
$conv = TBMsg::deleteConversation($conv_id, $user_id);
"Deletes" the conversation from a specifc user view.
$conv = TBMsg::isUserInConversation($conv_id, $user_id);
True or False if user is in conversation.
$conv = TBMsg::getNumOfUnreadMsgs($user_id);
return an integer of number of unread messages for specific user.
$conv = TBMsg::markReadAllMessagesInConversation($conv_id, $user_id);