Hi,
I am trying to create a small filter to send a firebase notification pending if the value isn't already in the MySQL database.
My B4J is receiving a heap of UDP messages all the time from devices in the field. (approx 30-40 per second, maybe more.)
Depending on the message that is received, will depend if it will send a firebase message. As I don't want to send the firebase notification every time, I want to check the MySQL database if the value exists and it will then send the push notification only if something else cleared the flag first.
Here is what I am doing:
Everytime the UDP message arrives, I am running the function like:
This will check the MySQL table and check if the user has subscribed to this topic.
If they have subscribed to this topic, it will check to see if this push notification was the last message to be sent for this ID & topic. If it was the last message for this ID & topic then it needs to ignore the message. (if the topic is in the database for this ID then ignore the message.)
If it's not the last message for this ID, then it will add this new flag to the MySQL database, and will delete the old flag.
Otherwise if I run the function multiple times with the same message, then it will send the firebase push notification multiple times, which I don't want.
So basically the topic needs to be different before it can send it again.
My B4A, B4i apps connect to this same B4J app and receives data and does other things in it.
When I run the above, the B4A/B4i apps get commands from this B4J very very slow. Seems to lag for some reason.
If I change the function like:
This seems to run fast and works. However it doesn't check to see if the push notifications needs to be sent or not and just sends it.
Can't work out why it's running slow when checking the MySQL server, which is running on the same server.
The B4A/B4i apps are communicating to this same B4J app and requesting data other data in the MySQL using my B4J app and it works fine.
For some reason this filter I created (as above) doesn't seem to work that well for some reason. It does work, but very very slow. 5 minutes later the push notification will come through.
I am using the MySQL database so I can save the flags incase I reboot the server etc and it can remember the values.
The only other thing I was thinking of was to load the values from the MySQL database into a map or list, when it first starts.
Then check the map/list rather than sending the request to the MySQL each time, but wasn't sure if this would speed things up, or would have the same result or run out of memory since this map/list could get quite big overtime.
The firebase_notifications table has 6540 rows.
Where the filter has 13156 rows.
Anyone got any ideas on how to make it work faster, or know of anything I might be doing wrong in my above code that could cause it to run slow ?
I am trying to create a small filter to send a firebase notification pending if the value isn't already in the MySQL database.
My B4J is receiving a heap of UDP messages all the time from devices in the field. (approx 30-40 per second, maybe more.)
Depending on the message that is received, will depend if it will send a firebase message. As I don't want to send the firebase notification every time, I want to check the MySQL database if the value exists and it will then send the push notification only if something else cleared the flag first.
Here is what I am doing:
B4X:
Sub CheckFirebase(ID As String, Topic As String) As Boolean
Dim returnValue As Boolean
Dim sql As SQL = pool.GetConnection
Try
'work with sql
Topic = Topic.ToUpperCase
Dim num As Int = sql.ExecQuerySingleResult("SELECT count(*) FROM firebase_notifications WHERE ID = '" & ID & "' AND topic = '" & Topic & "'")
If num = 0 Then
' no notifications enable
returnValue = False
Else
' notifications are enable
returnValue = True
End If
Catch
Log(LastException.Message)
End Try
sql.Close
' will return value
Return returnValue
End Sub
Sub RemoveFirebaseFilter(ID As String, topic As String)
Dim sql As SQL = pool.GetConnection
Try
'work with sql
sql.ExecNonQuery("DELETE FROM `filter` WHERE `ID` = '" & ID & "' AND `topic` = '" & topic.ToUpperCase & "'")
Catch
Log(LastException.Message)
End Try
sql.Close
End Sub
Sub AddFirebaseFilter(ID As String, topic As String)
Dim sql As SQL = pool.GetConnection
Try
'work with sql
sql.ExecNonQuery("INSERT INTO `filter`(`timestamp`,`ID`,`topic`) VALUES('" & DateTime.Now & "','" & ID & "','" & topic.ToUpperCase & "')")
Catch
'handle failure
End Try
sql.Close
End Sub
Everytime the UDP message arrives, I am running the function like:
B4X:
RunFirebase("12AB10012")
RunFirebase("12AB20012")
This will check the MySQL table and check if the user has subscribed to this topic.
If they have subscribed to this topic, it will check to see if this push notification was the last message to be sent for this ID & topic. If it was the last message for this ID & topic then it needs to ignore the message. (if the topic is in the database for this ID then ignore the message.)
If it's not the last message for this ID, then it will add this new flag to the MySQL database, and will delete the old flag.
Otherwise if I run the function multiple times with the same message, then it will send the firebase push notification multiple times, which I don't want.
So basically the topic needs to be different before it can send it again.
My B4A, B4i apps connect to this same B4J app and receives data and does other things in it.
When I run the above, the B4A/B4i apps get commands from this B4J very very slow. Seems to lag for some reason.
If I change the function like:
B4X:
Sub RunFirebase(msg As String)
If msg.SubString2(2,4) = "AB" Then
' message contains value
If msg.SubString2(4,5) = "1" Then
' check to see if the MySQL table contains this item
' run firebase function to send the push notification
' code here to send firebase notication (removed it while posting it to the forum since this part is working)
End If
If msg.SubString2(4,5) = "2" Then
' run firebase function to send the push notification
' code here to send firebase notication (removed it while posting it to the forum since this part is working)
End If
End If
End If
End Sub
This seems to run fast and works. However it doesn't check to see if the push notifications needs to be sent or not and just sends it.
Can't work out why it's running slow when checking the MySQL server, which is running on the same server.
The B4A/B4i apps are communicating to this same B4J app and requesting data other data in the MySQL using my B4J app and it works fine.
For some reason this filter I created (as above) doesn't seem to work that well for some reason. It does work, but very very slow. 5 minutes later the push notification will come through.
I am using the MySQL database so I can save the flags incase I reboot the server etc and it can remember the values.
The only other thing I was thinking of was to load the values from the MySQL database into a map or list, when it first starts.
Then check the map/list rather than sending the request to the MySQL each time, but wasn't sure if this would speed things up, or would have the same result or run out of memory since this map/list could get quite big overtime.
The firebase_notifications table has 6540 rows.
Where the filter has 13156 rows.
Anyone got any ideas on how to make it work faster, or know of anything I might be doing wrong in my above code that could cause it to run slow ?