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 trial

PHP

Rohin Kansal
PLUS
Rohin Kansal
Courses Plus Student 642 Points

How 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:

  1. What should be the database structure? 2 How should be the chat be updated everytime a new message comes?

4 Answers

This 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
PLUS
Emmanuel Salom
Courses Plus Student 8,320 Points

Yes, 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
Andrew Shook
31,709 Points

I was just going to recommend Ratchet so plus one to you, sir.

Rohin Kansal
Rohin Kansal
Courses Plus Student 642 Points

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.

Rohin Kansal
PLUS
Rohin Kansal
Courses Plus Student 642 Points

Thanks 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
Andrew Shook
31,709 Points

Rohin, 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.