I'm accessing the Firebase Real-time database from a SpringBoot App with privileged access to the database. That's why we use the JAVA Admin SDK. All is set up and works correctly. We use authentication without issues, and storing data works as well. The problem is in retrieving the data with a ValueEventListener
. The onDataChange
Event is simply never executed. When testing this I set multiple breakpoints, but these are not hit and neither are the logs or print statements outputted. From what I understand from the docs it should at least be called once with the initial data, which is all I need.
Our Springboot App is written in Kotlin and the relevant code is in the getRelatedData()
function. The insert works as expected and I verified the data has been inserted correctly. Thanks for the help already.
@Service
class AccountRelatedDataService(private val database: FirebaseDatabase) {
fun insertRelatedData(accountRelatedData: AccountRelatedData) {
val accountRelatedDataRef = database.getReference(refPath)
accountRelatedDataRef.child(accountRelatedData.fireBaseUID).setValueAsync(accountRelatedData).get()
}
fun getRelatedData(accountRelatedData: AccountRelatedData) {
val accountRelatedDataRef = database.getReference(refPath)
val listener = object : ValueEventListener {
@Override
override fun onDataChange(dataSnapshot: DataSnapshot) {
val data = dataSnapshot.getValue(AccountRelatedData::class.java)
log.info("In onDataChange method")
println(data)
}
@Override
override fun onCancelled(databaseError: DatabaseError) {
log.info("In onCancelled method")
println("The read failed: " + databaseError.code)
}
}
accountRelatedDataRef.addValueEventListener(listener)
}
companion object {
private val log = LoggerFactory.getLogger(AccountRelatedDataService::class.java)
private const val refPath = "accountRelatedData"
}
Inserting the data works fine on the same database object.
Note the database is wired in via Spring in the constructor. It is a Bean defined as:
@Bean
fun firebaseDatabase(): FirebaseDatabase {
return FirebaseDatabase.getInstance()
?: throw java.lang.IllegalStateException("Firebase database not available")
}
In response to the comment from Frank van Puffelen. For the insertRelatedData
I get normal logs like:
2021-09-02 20:58:21.619 DEBUG 4270 --- [database-worker] c.g.f.database.connection.Connection : [conn_0] Sending data: {t=d, d={a=p, r=2, b={p=accountRelatedData/1563c2f2-d95e-47d5-b0a8-6a63db617f13, d={cryptoWallets={ETH={ticker=ETH, address=0x0234b8305a7a821277c0b09951e5a536af0c4d24, validFrom={nano=860000000, epochSecond=1630609099}}}, fireBaseUID=1563c2f2-d95e-47d5-b0a8-6a63db617f13}}}}
2021-09-02 20:58:21.619 DEBUG 4270 --- [database-worker] c.g.f.d.connection.WebsocketConnection : [ws_0] Reset keepAlive. Remaining: 44998
2021-09-02 20:58:21.739 DEBUG 4270 --- [ebsocket-worker] c.g.f.d.connection.WebsocketConnection : [ws_0] WS message: {"t":"d","d":{"r":2,"b":{"s":"ok","d":""}}}
2021-09-02 20:58:21.739 DEBUG 4270 --- [database-worker] c.g.f.d.connection.WebsocketConnection : [ws_0] Reset keepAlive. Remaining: 44879
2021-09-02 20:58:21.739 DEBUG 4270 --- [database-worker] c.g.f.d.connection.WebsocketConnection : [ws_0] Handle new frame count: 1
2021-09-02 20:58:21.739 DEBUG 4270 --- [database-worker] c.g.f.d.connection.WebsocketConnection : [ws_0] Parsed complete frame: {t=d, d={r=2, b={s=ok, d=}}}
2021-09-02 20:58:21.739 DEBUG 4270 --- [database-worker] c.g.f.database.connection.Connection : [conn_0] Received data message: {r=2, b={s=ok, d=}}
2021-09-02 20:58:21.739 DEBUG 4270 --- [database-worker] c.g.f.d.connection.PersistentConnection : [pc_0] p response: {s=ok, d=}
For the getAccountRelatedData
I get no log statements at all from the Firebase client, despite log level being DEBUG.