You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use case :, I have multiple UI screens. I am not directly inserting into DB from UI. I am planning to use NATS Jetstream to capture user input and insert it into Postgres database.
Doing this POC to avoid DB deadlock when multiple users try accessing the DB.
Here is my sample code:
func JetStreamAccountInit(data string, subject string) {
nc, err := nats.Connect(nats.DefaultURL, nats.ReconnectWait(10*time.Second))
if err != nil {
log.Fatal(err)
}
defer nc.Close()
getStatusTxt := func(nc *nats.Conn) string {
switch nc.Status() {
case nats.CONNECTED:
return "Connected"
case nats.CLOSED:
return "Closed"
default:
return "Other"
}
}
log.Printf("The connection is %v\n", getStatusTxt(nc))
// Use the JetStream context to produce and consumer messages that have been persisted.
js, err := nc.JetStream(nats.PublishAsyncMaxPending(10000)) //ec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
log.Fatal(err)
}
// Create a stream if it does not exist
CreateStream(js, subject)
js.PublishAsync(subject, []byte(data))
select {
case <-js.PublishAsyncComplete():
case <-time.After(5 * time.Second):
fmt.Println("Did not resolve in time")
}
// Let's assume that publisher and consumer are services running on different servers.
// So run publisher and consumer asynchronously to see how it works
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
consume (js, account)
}()
wg.Wait()
}
In the example above, I am trying to push each UI page record into a specific subject
This is how I am pushing the data
Query #1:What can I use to verify if Jetstream is down.Is this enough nats.CONNECTED?
In the event, Jetstream shuts down , I want to alert users not to use the site.Is there any mechanism to save data temporarily while Jetstream reconnects.What can I refer to accomplish that?
Query #2 :In this CreateStream method show just below, I have apply check if subject not exists then create subject.However, stream == nil is not working and I believe it is always executing log.Printf("Creating stream: %s\n", subject) .I want to know what should be I using instead.
Query #3:I have been successfully able to publish and view messages.My need is to consume all messages published , acknowledge them ,read them one by one .
How can I write the code to continuously fetch all messages that are published, acknowledge them and read them one by one.Once read purge them after reading leaving other records intact
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Use case :, I have multiple UI screens. I am not directly inserting into DB from UI. I am planning to use NATS Jetstream to capture user input and insert it into Postgres database.
Doing this POC to avoid DB deadlock when multiple users try accessing the DB.
Here is my sample code:
In the example above, I am trying to push each UI page record into a specific subject
This is how I am pushing the data
JetStreamAccountInit(string(rec_mar), "Subjects.AccountInfo")
Query #1:What can I use to verify if Jetstream is down.Is this enough nats.CONNECTED?
In the event, Jetstream shuts down , I want to alert users not to use the site.Is there any mechanism to save data temporarily while Jetstream reconnects.What can I refer to accomplish that?
Query #2 :In this CreateStream method show just below, I have apply check if subject not exists then create subject.However, stream == nil is not working and I believe it is always executing log.Printf("Creating stream: %s\n", subject) .I want to know what should be I using instead.
Query #3:I have been successfully able to publish and view messages.My need is to consume all messages published , acknowledge them ,read them one by one .
How can I write the code to continuously fetch all messages that are published, acknowledge them and read them one by one.Once read purge them after reading leaving other records intact
Beta Was this translation helpful? Give feedback.
All reactions