Laravel private messaging system . Fetch all related users issue - laravel

I am referring to this thread
Laravel 4 Relationship: messaging system
I created the tables and also made the relations inside both model "Message" and "User".
Now i can fetch the list of messages sent by me through this command
user::find($myid)->sent_messages()->get();
MY QUESTIONS IS
How can we get list of users that has been messaging to me (logged in user) ?
I mean in the messages table "from" can be logged in user (1) or "to" can be logged in user (1) . I want to get list of other users that are chatting with the (logged in user) .
Hope you understand my question and help. I am new to laravel and trying to learn things and best practises. If you have suggestion to use any messaging system already there in github. I found one name "Laravel-messenger" but not suited to me.
Your suggestions and answer are highly appreciated. THANKS

May be instead of using table user_messages (just drop it) you just modify table Messages to below form:
Messages: id | from_user_id | to_user_id | content
Then you can make query:
$received = Messges::where('to_user_id','=',my_user_id);
$send = Messges::where('from_user_id','=',my_user_id);
$result = $received->union($send)->get();
(BONUS: you can also add column replay_from_message_id (null for first message) to save backward sequence of messages (thread). And then you can also add column first_message_id to simplify getting thread query)

Related

Don't show the message to all users? ( laravel )

I need to create to send a message to all new users registered on my website. I created a table called messages that admins can store (insert) the messages from the admin panel to this table and these messages are simply shown to all users with a foreach.
I don't know if this the best way to do something like that!
Anyway, the problem is that when any new user register and open his dashboard he just found the old messages
This is the table :
image
And this is the simple code for foreach
$msgForAll = Message::latest()->get();
I'm not sure how to display new messages to the users.
Again the way I made this idea is wrong, I know that ):
You can use eloquent's where spefically the whereDate queries in your case to get certain rows past or before date.
As an example relating to what you want to do, it can be along the lines of this:
// Given that you are using Auth to get the user's data...
// Get messages that were created after the user's creation date
$messages = Message::whereDate('created_at','>',Auth::User()->created_at)->get();

Messaging with two user tables in Laravel

I am developing a system in which it will have a chat system between two user tables: the clients and the engineers. My problem is how to make these two tables communicate in one message table with regard to registering the sender ID and recipient ID since both can change and are in different tables.
My initial thought for this was two tables where I would use the polymorphic relationship:
chats
id
client_id
engineer_id
messages
id
chat_id
senderable_id
senderable_type
receiverable_id
receiverable_type
content
What would be the most recommended way to solve this problem?
First of all, the following its the generic way that I see to create a chat schem. It'll works to a chat with 2 users or more than 2 users.
Note, the user_chat table its the pivot table between users and chats. You can define an alias to that table, like "participants", because it what it means. You can add more attributes to chats table, but basically need to know the creator.
Finally, the secret in messages table is just to get the sender id from users table, because sender and receiver are relative concepts in this context.
Another more simple and reduced way to do it (if you are trying to make a chat with only 2 users) is:

Logged In User Count Laravel 3

I'm using Laravel 3. How do I have a notice that can tell me how many users are currently logged in.
I wish I could explain this better but I am new to Laravel and I should upgrade to 5.1 but that is not a option at the moment.
Any help is appreciated.
Step 1: upgrade to Laravel 5.x
Step 2: add a migration to add the column active_at as a date to the users table
Step 3: add a new HTTP middleware called ActiveUser
Step 4: in the middleware handle method:
if ($user = $request->user()) {
$user->active_at = new \DateTime();
$user->save();
}
Step 5: feed your baby dragon
Step 6: you can now get users by activity using the active_at column
I think that is not a built in feature in Laravel. You could add a column to your user table, something like a flag if the user is logged in or not.
Simply set that column to true, after authenticating of the user and to false when the user logs out again.
Where you need the number of logged in users, simply count the rows in the user table where that flag is set to true.
As far as I know there is no easy way to achieve this. What you can do is show how many users have logged in in the last X minutes using the last logged in column in the users column. I admit, this isn't exactly what you're looking for but it might be the next best thing.
There is no way to accomplish this with laravel or php without using a third party tool like html5 websockets ( ratchet, socket.io etc ) or javascript with ajax.
it is pretty easy and more reliable with websockets. you can create your server to handle connection/disconnection and keep record of sockets connected and emit a message to all connected sockets count of actively connected sockets. This will be real time and less transaction since websockets will only trigger when there is an activity ( someone logins/logouts ) but requires higher access level of control over server/machine in order to install node.js, socket.io and open a tcp port for communication.
second option is javascript with ajax. keep a column in users data table as last_online and update this data each time user browses any page in your site. Like put a code at the top/bottom of your each page and update this columns value with current time. secondly determine how much avarage time a visitor roughly spends on your each page. then create a ajax script where you show your onlines count and trigger it every x amount of time that you determined ( i.e every 30 secs ) on the other side of your code check how many people were online since your last check ( current time - x time )
for instance
$query = "select count(id) total_online_last30 from users where last_check>='" . date( "Y-m-d H:i:s" , strtotime( date() . " +30 seconds") ) . "'"
then print it out.

Relational data query in Parse - retrieve users with and without messages

I have a relation on user in Parse 'messages'
I want to issue a query via cloudcoud that retrieves all friends of user (another relation, friendship) AND messages if there are any messages where the current user is the recipient.
I got this far:
var relation = current.relation("friendship");
var queryRel = relation.query();
queryRel.select("username","color","count");
queryRel.ascending("username");
var innerQuery = new Parse.Query("Message");
innerQuery.equalTo("recipient",current);
queryRel.matchesQuery("messages", innerQuery);
...but that only retrieves friends of the current user who have messages, not those who are friends but don't have messages. I need both!
If at all possible, I also want to retrieve the messages themselves, not just pointers. This is all so I can return properly organised data to the client so my apps don't have to do lots of looping to sort things.
I would use the include method here. The way it works is you would do queryRel.include("message"); so you won't just get the pointers. I believe you should also remove the second query for this to work.
The above code is assuming your column pointing to the Message table is called "message"

Comet chat integration with Code Igniter

I am using comet chat with Code Igniter 2.0.
Problem-
i have a listing page where all the users are listed and i need to show their comet chat status ("Online" or "Offline") next to their name.
For this purpose we have a DB field ('chat_status') in our users table, we usually set this field to '0' or '1' when user logged in we set this field to '1' and when user logged out we set this field to '0'. I just want to use comet chat call backs that will return all the online user_ids in array so i will change their 'chat_status' to '1' in our users table.
I couldn't find any comet chat function or query that will return all the online users in one shot.
Can you please assist me to solve this stuff.
Thanks in advance
There is a function in integration file [in my case its in its integration.php]
function getFriendsList() show all the friends on-line but if you want to show all the user insted of friends fellow the instruction given in link
You just have to edit the 3 sql queries in integration.php function by making changes to the username field .
You can write a subquery and get the status of the user that will be very easy .

Resources