简体   繁体   中英

Communication between browsers via PHP

I am building a simple ajax chat client for a school project and have thought of a way to implement this, but it seems IMO to be very cumbersome approach:

1) User A sends message which is accepted by a server-side PHP script and saved to database 2) The browser of User B periodically launches a server side PHP script to check if there are any messages in the database for User B. PHP script finds messages from User A and returns them.

Is this the right approach? Can communication between these two users be achieved without a database?

(This is my first web-application...if I was making this without browsers + HTTP, I would just make a Java program with persistent class that listened on TCP sockets, and forwarded messages to the appropriate address)

Yes your solution is good enough for starting. What you are doing is polling the server if there are any chat messages for a particular user. Good enough.

But if you wanna goto the next level, (probably could be tough), you can have a server which can push new messages onto client browsers. This is called "Comet". But it will need extensive server resources (if your userbase is going to exceed to thousands).

Try your method first and go for this next.

Each PHP "instance" only lasts for the duration of the request, so you need a persistent store such as a database for the chat messages.

And yes, I know that this does work because I've implemented a very similar system in the past.

The Comet Approach

Teehoo, if you want a working method, what you have suggested would be fine, especially if its just for a school project.

If you want something like the way that Facebook does it, you should look at commet HTTP connections. Its very clever. I remember when I first read about it I thought it was ingenious. It provides for fast updates and almost eliminates the AJAX dependency by constantly polling for new messages because you keep your connection to the web server constantly open.

Take a read of comet http connections, (but don't look at the comet chat application, they are a company trying to sell a product similar to the facebook chat application and isn't what you want.. although they have implemented the comet method).

Comet Chat #

http://en.wikipedia.org/wiki/Comet_(programming) )

Then read this:

http://www.zeitoun.net/articles/comet_and_php/start

But anyway, what you suggested is fine for a school project.

well yes is the answer. you can do it without a database.

But.. you will have to store the data on the central server some way. for a chat application a rational database isn't ideal for this chat type application but its only really relevant if you have a large site. if you are doing this for a project then a database would be plenty good enough to store the chat info. you just need to poll the database for new messages using javascript/ajax.

If you are interested in not using a database i'd suggest using a non-sql approach. Google is your friend on this as there are many options.

Is this the right approach? Can communication between these two users be achieved without a database?

You at least need a storage of some form - the persistence characteristics of the data is up to you. A database is a good way to persist the data for an extended amount of time.

You could also consider going through a shared memory storage eg memcache.

What you can also do is use a comet-like javascript approach. You keep a connection to a PHP page open from the browser until the PHP page receives a message.

However you are still limited to serving the PHP page per connection so you need some storage. If you want it to be really fast you could use memory.

Use memory in PHP: http://www.php.net/manual/en/function.apc-add.php

Comet approach to chat: http://www.zeitoun.net/articles/comet_and_php/start#comet_with_classic_ajax_litte_chat_demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM