Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRohin Kansal
Courses Plus Student 642 PointsHow to create a private chat application?
Hi. I want to develop a private chat application like facebook in PHP. I am not able to understand, how to start? Can anyone help me understand:
- What should be the database structure? 2 How should be the chat be updated everytime a new message comes?
4 Answers
Christian Andersson
8,712 PointsThis is not possible with php alone. php is only executed on the server before the browser loads the page. Once the page processed and the data sent to the client (browser), there is no more php going on. So making a live-chat via php alone is not possible, unless you are okay with "refreshing" the website every time.
You could do it the way you want it with php and combine either Ajax or javascript to check in regular intervals if there is a new message to be displayed.
As for the database, it depends what you want to save. Obviously you'd need to save the actual message in the database, but anything else is up to you. If you want to save who sent a message and when it was sent, you'll need the appropriate table structure for that.
Emmanuel Salom
Courses Plus Student 8,320 PointsYes, you can build real time applications with php. Take a look a the Ratchet library. here's a simple tutorial for you to get started: http://socketo.me/docs/hello-world
Andrew Shook
31,709 PointsI was just going to recommend Ratchet so plus one to you, sir.
Rohin Kansal
Courses Plus Student 642 PointsYes the user should be signed in and the chat would be saved in the database for later review by the user. Moreover, you can take the example of the facebook chat. I want similar to that.
Rohin Kansal
Courses Plus Student 642 PointsThanks everyone for your valuable suggestions . Yes the user should be signed in and the chat would be saved in the database for later review by the user. Moreover, you can take the example of the facebook chat. I want similar to that.
Andrew Shook
31,709 PointsRohin, could you post some more information regarding the functionality of what you want to build. I.e does a user need to be signed in, will you be keeping chat logs? This will help us make recommendations. Also, Emmanuel Salom, recommended Ratchet for routing chat info between sites, but you could also use Node.js. Here is a good goggle group discussion comparing the two.