Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
madsongr opened this issue Jul 8, 2024 · 5 comments
Open

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

madsongr opened this issue Jul 8, 2024 · 5 comments

Comments

@madsongr
Copy link

madsongr commented Jul 8, 2024

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
Copy link
Collaborator

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
Copy link
Author

madsongr commented Jul 8, 2024

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
Copy link
Collaborator

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:

  • Start the Socket server that will be responsible for receiving connections from your users.
  • In this server, you can create a loop that runs every 5 seconds, or 10 seconds, or any other interval. In this loop, you would query a queue, I suggest Redis for example, or a table in MySQL, no problem.
  • On your web panel, when inserting a record into any table, create a message according to your structure and store it in the queue.
  • Then, in your loop, query this queue, send the messages to your connected users, and remove the message from the queue.
  • This way, you will have asynchronous and real-time processing.

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

@madsongr
Copy link
Author

@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?

@marcosmarcolin
Copy link
Collaborator

Hello @madsongr,

sorry for the delay.

But no, you don't need Node, this repository is for exactly that, to use PHP.

What you need is to create a socket.io PHP Server where the clients (front-end) will connect.

See an example here: https://github.com/marcosmarcolin/async-php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants