So I want to develop a chat system based on nodejs and socket.io, I have made a prototype and it works, the only thing that is stuck in my mind is how to store the chat messages in the database.
I guess is not a good idea to store a message when a user hits the enter button, because this is live chat you could have 1000 just from a user in 30-60 min.
The question is WHEN to store the data in the database, because I don't think that storing right away when the user hits enter will work on the long term?
The chat works on the same idea as facebook.
If you are not saving the messages at the moment, how do you plan to save them when you want to?
The messages sent have been delivered to the client and your server no longer has them, and you can't use the client to store them in a database.
You need to store the messages as the user sends them.
Related
Goal:- Inserting messages in faster way to database.
I have integrated xmpp openfire one-to-one chat over my website.
I am using Monitoring Service plugin for storing messages into the database.
What i am facing is when user send a message from one user to another user at that time messages are stored in the database.
Now messages are stored, but i am facing issue of inserting messages late into the database.
I am loading messages via ajax call when user clicks on another user message thread but if the messages does not store in the database ofcourse it will not be returned in ajax call history.
I have done following configuration settings for Monitoring Service plugin.
I've find alternative solution of modifying Monitoring Service plugin's jar file and decrements inserting seconds but ultimately it will affect database.
Eg. Every minute 50 users send 10 messages at that time database may hanged.
Any help would be appreciated if i can store the messages into the database as faster way!
I'm building a web application using PHP, JavaScript/jQuery and MySQL. Right now I'm trying to implement a chat feature that allows two user who are both online to chat with each other (e.g. NOT a big chat room, but only private chats between two users). However, I've ran into the following questions during the implementation process:
How can I let one user know whether another user is currently online or not? Now I have a page where a user can see the name of other registered users. I hope to differentiate those who are currently logged in from those that aren't. Currently, when a user is logged in, I store his username in $_SESSION['name']. So how can one user know whether another user's $_SESSION['name'] is also set?
How can I ensure that the conversation is private to two users? I currently have a page called "chat.php", where the chat interface is located in. When one user clicks on the name of another user who's also online, the two will be directed to their own "chat.php". Similarly other users should be unable to view chat that they are not involved in. I'm currently thinking about generating a unique page for the two users, like "chat.php?user1=Tom&user2=John" But how exactly should I achieve this? I'm new to PHP.
To display the new message if the other person has just sent one, can we do this using Ajax in an event-driven way? Or can we only use polling? I'm currently using polling like the following, but I feel that polling isn't every efficient:
// "logs.php" reads chat message from the database
setInterval(function(){
$.get("logs.php", {}, function(resp) {
// display the response
});
}, 1000); // poll every second
Any input to any of the above questions is appreciated! Thanks.
I am also currently working on chat right now, below answer are according to my best of knowledge if you find something wrong please comment it.
Answer 1//
With the use of session you will not be able to get who are logged in and who aren't, to check you have to set flag in database and from there you to fetch logged in user apart from current session user.
Answer 2//
Here you are mixing to different concept of chat i.e. one to one chat(private chate) and another is group chat. What I will prefer is create different chat file for both concept.
Consider a scenario where you 100 user chatting in group you will not be able to send 100 user id through ajax.
Answer 3//
Long polling is the method with which you can achieve real time chat update. But still there are so many catch to use long polling with PHP. First and foremost is the main drawback using long polling is it use more resource when user will increase.
So, in conclusion try to use web socket programming or use different framework like node.js to implement.
If you have less number of user then definitely you can use long polling with PHP.
Basically I have a chat, using AJAX, PHP.
I let guests login by filling their name and clicking login.
It fills the session $_SESSION['guest'].
Now, when they close their browsers, or something, once the session ends, I want it to say in the chat
"The username has left the chat".
But I am not sure on how would I do this.
Any ways to do it? Maybe I could fill an array of users that were active in the past 5 minutes, if not, remove from the array and it will kick them off the chat.
Any ideas?
You could have an array of users in the backend with the timestamp of the last ping.
Then, the clients end can update the backend using ajax by sending a new timestamp every so many seconds.
Whilst your backend is being updated by one of the users, your backend can do a check on all users checking the last timestamp sent to the current time, if its over 2 minutes or something you can tell they have left the chat and output the message.
Without a direct connection to the browser, you won't be able to actively tell if they're connected. I'd suggest a ping-like solution, requiring the browser to ping the server every n seconds to keep the chat "alive".
If the browser doesn't "check in" with the server, the server assumes they've disconnected and reports back to the other participant.
That said, if you're able to dive a little deeper, Google "javascript real-time chat" and you'll have several paths to success.
Add a jQuery .unload() event to your chat client. When they navigate away from the chat it will be fired and you can use it to send the last bit of data to display that message.
Here is the documentation: Link.
Example:
$(window).unload(function() {
//send message to server informing it of client leaving
});
I am creating a messaging system which have the following parts:
A form which send user message and upon submission of the form, PHP inserts the data into a MySQL Table called userMessages.
A PHP page which performs a MySQL Query select all from userMessages and displays all the messages.
The problem I'm having is making this messaging system have an INSTANT Message Functionality. i.e. I submit data from one form and it instantly appears on the user messages page WITHOUT having to manually refresh the page.
I do have temporary solution of refreshing the page every 20 second. But is there a way to update the messages page only at the moment a new message is submitted?
Pushing data to a web page is very hard, as Dan Grossman has said you will want to read that wiki article. AJAX polling every few seconds would be a good idea, if you don't mind a rather high server and database load. ama2 is also right - PHP is inherently not the best system for this, and a continuously running application server using, say, node.js, may be a lot more efficient.
I'm making a website similar to Facebook - with things such as Notifications which make it similar. I don't really see how I can get notifications working. Well, I figured out how I could get them from the database, with a query.
The structure I think the site will follow would be multiple tables for different applications - ie: Photos would add in an ID for the picture, a filename, and a few user IDs if 'tagged' or something, and how to send that information to the user in real time.. is beyond me.
So I would have to run several queries every few seconds scanning the database tables for the $_SESSION['id'] of the user being found in all the applications tables with a status of unread?
Another possibility is that every user has their own table? That's .. a lot. lol.
Or just a notifications table with the most recent notification being pushed to the table with a unique id and a user id?
I really can't wrap my head around this, lol.
Also, displaying notifications in real time? I understand Facebook uses long-polling and gets the notifications in real time, but I don't think I could leave about 5-10 queries (for each app) running on a long poll for multiple clients, or that'd completely crash my server, right?
Any advice/code on how I could try and make a notification system for a social networking-ish site? If not, I think i'll go with static notifications rather than any sort of realtime.
Then again, that'd be too much load querying the server every few seconds for a new notification on every page load? Using ajax would mean long polling, so it's a lose-lose.
I would say Long polling is the answer. Gmail and Facebook both use this method for real-time notifications. Your only other alternative is Flex with a dataservice, but that is not PHP.
In terms of performance, the query is only going to pull from 0-5 notifications at a time, and if the tables are indexed properly, and the query is written well, then 5 of these queries will not be a significant impact on your server.
Furthermore, if Gmail and facebook are doing it, then it stands to reason you can also do this. Granted, they have a ton of servers to support all their users, but I am going to go out on a limb and say you don't have as many users as they do, so as a result the server technology will work for now. And when you get so many users your current servers can't handle the load, then you invest in newer more powerful ones.
Well here is my take on it.
You could create different tables for status, photos and videos.
Everytime somebody comments on a video or something you can do the query to store the notification along with the information of the user who liked it, you should set a status field too, so you can query based on which has been seen and which has not been seen by the user.
You can put the url of the page where the photo is or status is located so when the user is logged in you do a query every five minutes checking for unread notifications, if there are any you display them in a tiny toast message on left bottom side of the screen like facebook.
On click of the toast message you can do an ajax call to update that status of the notifcation to read so it does not show up again and in the success call back you can take the user to the page where the status update is.