staticbackendhq / core

Backend server API handling user mgmt, database, storage and real-time component
https://staticbackend.com
MIT License
700 stars 66 forks source link

chore: add local cache sub/pub events #49

Closed rostikts closed 2 years ago

rostikts commented 2 years ago

Add a local observer for the Pub/sub cache events #42

Observer provides a possibility to subscribe to a specific event and publish a message for all subscribers of the channel. The memObserver struct contains a map with a slice of subscribers where the key of the map is a specific channel.

The subscriber is basically a wrapper for the channel that is listening for the changes

rostikts commented 2 years ago

@dstpierre Hi, the task is still in progress, I'll update the logic according to the Redis cache, but the core of the dev SubPub is done, could you take a look at the Observer implementation?

Also, I want to clarify a few things:

  1. I'm confused about these two conditions:
    
            if msg.Type == internal.MsgTypeChanIn {
                msg.Type = internal.MsgTypeChanOut
            } else if msg.IsSystemEvent {

Should there be some business logic that I'm not aware of? 
dstpierre commented 2 years ago

Yep, you're deep in SB event / messaging aspect.

This is to prevent an infinite loop for the following scenario:

  1. The user would subscribe to a topic and have a server-side function run on new message.
  2. If they were to push to that very own topic from the function, it would get re-trigger without the "SystemEvent" type. Imagine a live chat for example, where the intent is to call a server-side function for each messages in a topic.

This entire part of StaticBackend (event / messages) is still in its early design phase.

dstpierre commented 2 years ago

@rostikts this look good to me so far.

One small thing, I'd rename the dev_observer package either just observer or devobserver if we need to keep the "dev", but we could lose it IMO. Same for the dev_cache_observer.go, i'd just name it observer.go in the observer package.

Underscore in package name is to be avoided as much as possible.

Am I understanding correctly that the remaining implementation is for the PublishDocument function and this would be ready for more test?

Thanks for this PR by the way :wave:

rostikts commented 2 years ago

@dstpierre Yep, it's still in progress. You're right, the PublishDocumentfunction and more tests are planned in the next iteration, it should be ready today, maybe Saturday. Also, I should review the func (o *memObserver) Publish(channel string, msg interface{}) error function, I guess I should add here timer with some timeout after which the existing message in the channel will be discarded to avoid the blocking