walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.29k stars 509 forks source link

Watch database table and send updates to React Native app #305

Open madsongr opened 2 months ago

madsongr commented 2 months ago

I have a RN app that gets data from mysql database. I fetch that data using php and send it to the app in json format.

I'd like to watch a specific table to check if new data is inserted on it so that I can update my list in RN app. Is that possible using phpsocket.io or I should use a node server to do that? I wouldn't like to use node because my client's hosting plan has additional charge over it.

marcosmarcolin commented 2 months ago

Hi @madsongr,

If I understand your question, when there is a new insertion in a table you want to notify your application, right?

What I understand is the following: the only function of PHPSocket.io is to send and receive messages, you need to implement table monitoring from the outside.

Suggestions:

  1. You can make a task run via Crontab that queries the table from time to time and notifies the application if there are changes.
  2. If you want this in real time, you will need to intercept the code where the insertion is made and notify your application instantly. Here, you can create a queue system and send it directly to your application, without the application needing to query the database. It's a little more work at this point.
  3. You can make the PHPSocket.io server query the data periodically and send it to your application.

I have already applied all of the suggestions above and obtained great results.

I hope it helps something.

madsongr commented 2 months ago

Hi, @marcosmarcolin. Yes, you got it.

Ok. I thought websocket could watch the db by itself. It's my first time trying to implement it.

My client uses a dashboard (reactJS frontend) that creates that data. When my client creates a new item, my php makes the insertion in the backend and sends a push notification to my app (RN app) via PHP after insertion is successfull.

Do you think I could implement websocket at this point too? I mean, after insertion. Do you know any example I couild use if the answer is yes?

PS: I'm also from SC, BR. hehe

marcosmarcolin commented 1 month ago

Hey @madsongr , awesome to see BR here. (:

Alright, let's go, I'll suggest something that I've already implemented and it worked well.

Just reinforcing, WebSocket is different from Socket.io, even though they do similar things.

This project is an implementation of Socket.io from Node.js. If you are depending on WebSocket, check this out: http://socketo.me/.

So, here's what I would do:

You can do the same process with WebSocket, only the implementation will change.

madsongr commented 1 month ago

@marcosmarcolin Ok. I've been studying socket.io this week. As far as I noticed I must run a node server along with my php server to make the handshake, right?! If so, I don't know if it's gonna be possible to use it because my client hosting doesn't have node avaliable.

Could you clarify this telling me if what I'm saying is correct?