Communication between browsers through PHP

I am creating a simple ajax chat client for a school project and thought about how to implement it, but IMO seems to be a very cumbersome approach:

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

Is this the right approach? Is it possible to provide communication between these two users without a database?

(This is my first web application ... if I did it without browsers + HTTP, I would just make a Java program with a constant class that listened to TCP sockets and forwarded messages to the appropriate address)

+4
source share
6 answers

Yes, your decision is enough to start. What you are doing is polling the server if there are any chat messages for a particular user. Good enough.

But if you want to go to the next level (maybe it can be tough), you may have a server that can trigger new messages in client browsers. This is called a comet. But this will require extensive server resources (if your database will exceed thousands).

First try your method and move on to the next one.

+3
source

Each PHP β€œinstance” only lasts for the duration of the request, so you need persistent storage such as a database for chat messages.

And yes, I know that this works because in the past I implemented a very similar system.

+2
source

The approach to comets

Teehoo, if you want to use a working method, then what you suggested will be fine, especially if it is only for a school project.

If you want something like Facebook does, you should look at commet's HTTP connections. This is very smart. I remember when I first read about it, I thought it was brilliant. It provides fast updates and almost eliminates AJAX dependency by constantly polling new messages because you are constantly opening your connection to the web server.

Take a look at the 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 not what you want. Although they have implemented a comet method).

Comet Chat #

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

Then read the following:

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

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

+2
source

Well, yes, the answer. you can do it without a database.

But .. you have to somehow store the data on a central server. for a chat application, a rational database is not ideal for this type of chat application, but it is really relevant if you have a large site. if you do this for a project then the database will be good enough to store chat information. you just need to query the database for new posts using javascript / ajax.

If you are interested in not using a database, I would suggest using a non-sql approach. Google is your friend as there are many options.

0
source

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

At the very least, you need to store some form - the characteristics of data persistence are up to you. A database is a good way to store data for a long time.

You might also consider using shared memory storage, for example. Memcache

0
source

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

However, you are still limited to serving the PHP page for each connection, so you need some storage. If you want it to be very 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

0
source

Source: https://habr.com/ru/post/1301818/


All Articles