deepstreamIO / deepstream.io

deepstream.io server
https://deepstreamio.github.io
MIT License
7.13k stars 382 forks source link

Is there a simple way to not receive message send from self? #1130

Closed geohuz closed 1 year ago

geohuz commented 1 year ago

Is there a server setting which make sure a provider client doesn't get the change to record/list by itself?

jaime-ez commented 1 year ago

you mean that you change a record data but don't want to susbcribe to it? if so, you should use client.record.setData , look the docs here

geohuz commented 1 year ago

Well, not exactly, say if I subscribed to a data and also changed it, I will still get call back from my changes, what I want is exlcude any change initiated from self site but still receive any message from other sides. I'm thinking this behavior will cause dead loop in data operation.

jaime-ez commented 1 year ago

Ok, no you can't do that currently at the server level. Data changes are broadcasted without informing which user made the change. My advice is to review your client side logic in order to avoid loops.

geohuz commented 1 year ago

All right, currently I'm using the client.record.has(recordName) as a way to check the client memory if the record has been fetched, something like:

  let localRecordContent = {}
  let localRecordExist = await client.record.has(recordName)
  if (localRecordExist) {
    localRecordContent = client.record.getRecord(recordName).get()
  } 

not sure if this works.

jaime-ez commented 1 year ago

You're mixing concepts: client.record.has tells you wether the record exists in the server (including database/cache if you're using)

If you want to keep track of records at the client level, one way is to use a singleton pattern:

const client = new DeepstreamClient()
const currentRecords = new Map()
const getRecord = (recordName) => {
  let record = currentRecords.get(recordName)
  if (!record) {
    record = client.record.getRecord(recordName)
    currentRecords.set(recordName, record)
  }
  return record
}
const discardRecord = (recordName) => {
  const record = getRecord(recordName)
  record.discard()
  currentRecords.delete(recordName)
}
const deleteRecord = (recordName) => {
  const record = getRecord(recordName)
  record.delete()
  currentRecords.delete(recordName)
}
geohuz commented 1 year ago

Thank you @jaime-ez ! That should solve the problem!