PHP Mysql broadcast Message System - php

I'm trying to build a messaging system. The system that I create is only able to send message to a user. Trying to make a broadcast system and now I could send to multiple users. But now the problem is how I could make a notification when it's a new broadcast message, and how I could delete the broadcast message?
NB : When I delete the broadcast message with a user(example the user I use is 'user1'), the broadcast message will also deleted in another user account.
Can anybody help me how to solve this problem please?
EDIT
Here I got some table in my database
Table Message
id_message (INT(11))
to (VARCHAR(50))
from (INT(11))
title (TEXT)
user1delete (TINYINT(4))
user2delete (TINYINT(4))
Table Detail_Message
id_message (INT(11))
id_reply (INT(11))
from (INT(11))
message (TEXT)
user1read (TINYINT(4))
user2read (TINYINT(4))
Table Broadcast_Delete_Status
id_message (INT(11))
user (INT(11))
when the message is pointed to single user, the column 'to' in table message save the id of the recipient, and when it is a multiple or broadcast message, the column save "all user" or "all operator".

Related

How to design a database schema for a messaging app?

I'm creating a laravel website which has Messaging as an included feature.
I would like to take into consideration the following factors.
Both parties (the sender and the receiver) have the ability to delete the messages in their end.
if any of the parties deleted the message the other party should still having access to the message.
the message might have attachments.
I thought of the following schema but I think it has too much duplication and I'm looking for something better if any.
messages table:
-----------------
id
title
body
received_messages table:
------------------------
message_id
receiver_id
sender_id
is_read
sent_messages table:
------------------------
message_id
receiver_id
sender_id
Edit: Another schema came to my mind:
messages table:
-----------------
id
title
body
user_messages table:
-----------------
message_id
sender_id
receiver_id
sender_del // indicates that the sender deleted the message in their end
receiver_del // indicates that the receiver deleted the message in their end
read_at // timestamp null by default
When one party choose delete the message the corresponding field (sender_del, receiver_del) will be true so that the message will not be shown to that person
Using the previous schema the message will only be deleted from the DB if both parties deleted it
<?php
$user = auth()->user();
$received = $user->receivedMessages; // to get received messages
$sent = $user->sentMessages; // to get sent messages
any suggested modifications, which schema to use or a whole new schema are welcomed.
Thanks.

How to display push message in firefox when mysql table has a new record

I have a mysql table named notification with this structure
id user_id time name
I want to display a push message when this table has been updated if user_id is same width user id of that user has been logged in
but i don't know how to do this
I tried to do that with this tutorial https://wiki.mozilla.org/WebAPI/SimplePush but i failed
Is there any other way to play a sound and show the notification ? please help me about this problem

Managing Read and Unread messages with php and mysql

I created a chat application with php and mysql. Where users can send messages to each other.
Now I want to implement a "read" and "unread" messages system.
Forexample Title of A unread message should look "black" and Title of a read message "Gray".
My table structure :
MessageId SentTo SentBy Created Status
Status column is for message status (Read|Unread) by default Unread.
I have a readmsg.php file When a user clicks on Message title in Inbox.php he is redirected to readmsg.php and there he can read the full msg. there also an Insert Into statement runs, this updates the Status column with Read .
Readmsg.php
$mid=$_GET["msgid"];
$you=$_COOKIE['username'];
$st= "SELECT*FROM msgbox WHERE sentto='$you' AND id='$mid' LIMIT 1";
#Script to update Status column
This all works but It's really very hard to manage .
Is there an easy way to manage read and unread messages with php and mysql?
Any help is much appriciated.

Implementing threaded message system in php

I have a basic private messaging system and I am trying to add a threaded message feature, so the users can msg each other without starting a new msg.
I have two tables one for the messages and the other for the conversation.
What I am actually doing is that I have inbox.php where I show all the messages, then I have view.php where I show the message detail and on that page I have a reply box so if the user sends a reply the new message will be inserted to conversation table with the parent id being inserted to message_id column in conversation table.
The problem
As I said I am inserting the reply message to conversation table and now user2 won't be able to see the reply in his inbox as I am fetching that data from messages table.
I tried inserting the reply to both messages and conversation table but that will insert it as a new id and the message_id will be different.
So how would I be able to implement this messaging system where the reply can go to both users?
messages table
id
from_user
to_user
message
date
conversation table
id
from
to
message
message_id
date
Have One table for all your messages and just expand it to accommodate one more field for which message id the message was being sent to.
id
user
to_user
message
date
in_reply_to_messageId

Private Messaging Notification - Database Table Design

I have have created a Private messaging system using PHP and mySQL with notification a bit like Facebook.
The database table has following fields( not all listed):
MessageID
senderUserID
RecUserID
Message
Subject
DateTime
Status - whether read or not
RepliedStatus - how should i use this?
DeleteRec - delete from inbox
DelSender - delete sender inbox
RepliedUserId - When user reply to orginal message this is change to receiver's id
All replies are stored in a second table, since each message create a thread. The second table looks a bit like this:
messageID - FK
repuserID
Mesage
DateTime
At the moment when a new message is sent out to a user i change the 'status' of the message to unread, From this can run a count query to list all of the unread messages in notification.
But if the users replies back to that message i cant set the original 'status' field to unread, since this will appear on both users notification. so i created another field called 'RepliedStatus ' but i am unsure how would i use this to show notification on message reply?
thanks guys.
If you have a replies table then you don't need a replied status column on your first status. By virtue of there existing a record in the replies table you know that a user has replied to a message
Why dont you add an INT and nullable column to the first table (let's say, "messages" table) named "previous_message"?
ALTER TABLE messages ADD COLUMN previous_message INT DEFAULT NULL;
So every message will have in the same table the previous one, and you can work out the sequence. If it helps you can have a "next_message" column with same definition and update the relevant record on reply.
Doing so you can use the status column on each reply.
If you want to keep the same DB organisation I would suggest to add a column status on the second table (let's say "replies").
Hope this helps
Me I'll put deleted column once and the same things for the read or not-read like:
[{"0":"both", "1":"Sender", "2":"receiver"}];
And then fetching a tread messaging like:
$sql = "SELECT * FROM messagetreads
WHERE (senderID OR receiverID = ".$_SESSION['MyCurrentId'].")
AND deleted !== 0
ORDER by TreadID AND DateTime ASC";
When a sender "delete" is tread... All tread relatedID in the database change for 1 or 0 if delete colomn is 2...
But I think it's better to creat another colomn for getting of the repeated deleted and notifications data like
TreadID (FK_message_Table)
delete (0=both deleted UserID=don't appear to this sender or receiver)
notify (0=both read UserID=read to this sender or receiver)
Then it's ezee to change a status or a delete statement.
The SELECT will be sometings like this:
$SQL = "SELECT *
FROM messagetreads
WHERE (senderID OR receiverID = ".$_SESSION['MyCurrentId'].")
IN (SELECT TreadID WHERE delete !== (0 OR ".$_SESSION['MyCurrentId']."))";
If is our member id involve in the colomn delete all the tread don't appear but if is the id of the receiver when the sender will delete 0 can be attributed so that both of the members can "delete" the message. Same thing for notifications!
very far later a cron-job can come delete very-old message read(0)...
PS: this can be notification system too like notifying for a new post in wall or comment on a photos or new events on calendar ... just add more information in column data and faormat it with php or java-ajaxed alike...

Categories