Skip to content

Commit 534fd30

Browse files
OMGeekybfopsgefjon
authored
Fix Rust Chat App Tutorial not showing messages of other users live (#4588)
# Description of Changes This fixes an Issue in the Rust [Chat App Tutorial](https://spacetimedb.com/docs/tutorials/chat-app#creating-the-client), that is caused by the [Event Type Changes](https://spacetimedb.com/docs/upgrade/?client-language=rust&server-language=rust#event-type-changes) in 2.0 This resulted in other clients not receiving new messages, since they are now Event::Transaction and no longer included within the Event::Reducer # API and ABI breaking changes <!-- If this is an API or ABI breaking change, please apply the corresponding GitHub label. --> I don't think any? I only changed a tutorial, the real change was in 2.0 # Expected complexity level and risk 1 # Testing - [x] I ran the Tutorial app with these changes and messages started to appear as expected on the second client when writing something on the first. There might be more places where this is an Issue, I just noticed this one while following the Tutorial. --------- Signed-off-by: Frederik <39029799+OMGeeky@users.noreply.github.com> Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com> Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
1 parent 7a7860d commit 534fd30

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

docs/docs/00100-intro/00300-tutorials/00100-chat-app.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@ fn on_user_updated(_ctx: &EventContext, old: &User, new: &User) {
25562556

25572557
#### Print messages
25582558

2559-
When we receive a new message, we'll print it to standard output, along with the name of the user who sent it. Keep in mind that we only want to do this for new messages, i.e. those inserted by a `send_message` reducer invocation. We have to handle the backlog we receive when our subscription is initialized separately, to ensure they're printed in the correct order. To that effect, our `on_message_inserted` callback will check if the ctx.event type is an `Event::Reducer`, and only print in that case.
2559+
When we receive a new message, we'll print it to standard output, along with the name of the user who sent it. Keep in mind that we only want to do this for new messages, i.e. those inserted by a `send_message` reducer invocation. We have to handle the backlog we receive when our subscription is initialized separately, to ensure they're printed in the correct order. To that effect, our `on_message_inserted` callback will check if the ctx.event type is an `Event::Reducer` (new local messages) or `Event::Transaction` (new messages from others), and only print in that case.
25602560

25612561
To find the `User` based on the message's `sender` identity, we'll use `ctx.db.user().identity().find(..)`, which behaves like the same function on the server.
25622562

@@ -2569,7 +2569,7 @@ To `src/main.rs`, add:
25692569
```rust
25702570
/// Our `Message::on_insert` callback: print new messages.
25712571
fn on_message_inserted(ctx: &EventContext, message: &Message) {
2572-
if let Event::Reducer(_) = ctx.event {
2572+
if matches!(ctx.event, Event::Reducer(_) | Event::Transaction) {
25732573
print_message(ctx, message)
25742574
}
25752575
}

templates/chat-console-rs/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn on_user_updated(_ctx: &EventContext, old: &User, new: &User) {
140140

141141
/// Our `Message::on_insert` callback: print new messages.
142142
fn on_message_inserted(ctx: &EventContext, message: &Message) {
143-
if !matches!(ctx.event, Event::SubscribeApplied) {
143+
if matches!(ctx.event, Event::Reducer(_) | Event::Transaction) {
144144
print_message(ctx, message);
145145
}
146146
}

0 commit comments

Comments
 (0)