Skip to content

Commit bdd0e3f

Browse files
committed
Fix untilAttach examples to contain subscribe
1 parent 903064c commit bdd0e3f

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

src/pages/docs/storage-history/history.mdx

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,12 @@ Use the `untilAttach` option when making history requests on attached channels.
444444
const realtime = new Ably.Realtime('{{API_KEY}}');
445445
const channel = realtime.channels.get('{{RANDOM_CHANNEL_NAME}}');
446446
await channel.publish('example', 'message data');
447-
await channel.attach();
447+
448+
channel.subscribe((message) => {
449+
console.log('Received message: ' + message.data);
450+
});
451+
452+
await channel.whenState('attached');
448453
449454
const history = await channel.history({untilAttach: true});
450455
const lastMessage = history.items[0];
@@ -455,7 +460,12 @@ console.log('Last message before attach: ' + lastMessage.data);
455460
const realtime = new Ably.Realtime('{{API_KEY}}');
456461
const channel = realtime.channels.get('{{RANDOM_CHANNEL_NAME}}');
457462
await channel.publish('example', 'message data');
458-
await channel.attach();
463+
464+
channel.subscribe((message) => {
465+
console.log('Received message: ' + message.data);
466+
});
467+
468+
await channel.whenState('attached');
459469
460470
const history = await channel.history({untilAttach: true});
461471
const lastMessage = history.items[0];
@@ -465,7 +475,12 @@ console.log('Last message before attach: ' + lastMessage.data);
465475
```realtime_ruby
466476
realtime = Ably::Realtime.new('{{API_KEY}}')
467477
channel = realtime.channels.get('{{RANDOM_CHANNEL_NAME}}')
468-
channel.attach do
478+
479+
channel.subscribe do |message|
480+
puts "Received message: #{message.data}"
481+
end
482+
483+
channel.on(:attached) do
469484
channel.history(until_attach: true) do |result_page|
470485
last_message = result_page.items.last
471486
puts "Last message before attach: #{last_message.data}")
@@ -476,6 +491,14 @@ console.log('Last message before attach: ' + lastMessage.data);
476491
```realtime_java
477492
AblyRealtime realtime = new AblyRealtime("{{API_KEY}}");
478493
Channel channel = realtime.channels.get("{{RANDOM_CHANNEL_NAME}}");
494+
495+
channel.subscribe(new MessageListener() {
496+
@Override
497+
public void onMessage(Message message) {
498+
System.out.println("Received message: " + message.data);
499+
}
500+
});
501+
479502
channel.on(ChannelState.attached, new ChannelStateListener() {
480503
@Override
481504
public void onChannelStateChanged(ChannelStateChange stateChange) {
@@ -490,37 +513,49 @@ console.log('Last message before attach: ' + lastMessage.data);
490513
}
491514
}
492515
});
493-
494-
channel.attach();
495516
```
496517

497518
```realtime_csharp
498519
AblyRealtime realtime = new AblyRealtime("{{API_KEY}}");
499520
IRealtimeChannel channel = realtime.Channels.Get("{{RANDOM_CHANNEL_NAME}}");
500-
await channel.AttachAsync();
521+
522+
channel.Subscribe(message => {
523+
Console.WriteLine("Received message: " + message.Data);
524+
});
525+
526+
await channel.WaitForAttachAsync();
527+
501528
PaginatedResult<Message> resultPage = await channel.HistoryAsync(untilAttach: true);
502529
Message lastMessage = resultPage.Items[0];
503-
Console.WriteLine("Last message before attach: " + lastMessage.data);
530+
Console.WriteLine("Last message before attach: " + lastMessage.Data);
504531
```
505532

506533
```realtime_objc
507534
ARTRealtime *realtime = [[ARTRealtime alloc] initWithKey:@"{{API_KEY}}"];
508535
ARTRealtimeChannel *channel = [realtime.channels get:@"RANDOM_CHANNEL_NAME"];
509-
[channel attach];
536+
537+
[channel subscribe:^(ARTMessage *message) {
538+
NSLog(@"Received message: %@", message.data);
539+
}];
540+
510541
[channel on:ARTChannelEventAttached callback:^(ARTErrorInfo *error) {
511542
ARTRealtimeHistoryQuery *query = [[ARTRealtimeHistoryQuery alloc] init];
512543
query.untilAttach = YES;
513544
[channel history:query callback:^(ARTPaginatedResult<ARTMessage *> *resultPage, ARTErrorInfo *error) {
514545
ARTMessage *lastMessage = resultPage.items[0];
515-
NSLog(@"Last message: %@ - %@", lastMessage.id,lastMessage.data);
546+
NSLog(@"Last message before attach: %@ - %@", lastMessage.id, lastMessage.data);
516547
} error:nil];
517548
}];
518549
```
519550

520551
```realtime_swift
521552
let realtime = ARTRealtime(key: "{{API_KEY}}")
522553
let channel = realtime.channels.get("{{RANDOM_CHANNEL_NAME}}")
523-
channel.attach()
554+
555+
channel.subscribe { message in
556+
print("Received message: \(message.data)")
557+
}
558+
524559
channel.on(.attached) { error in
525560
let query = ARTRealtimeHistoryQuery()
526561
query.untilAttach = true
@@ -538,24 +573,26 @@ if err != nil {
538573
log.Fatalf("Failed to create Ably Realtime client: %v", err)
539574
}
540575
541-
// Get the channel
542576
channel := client.Channels.Get("{{RANDOM_CHANNEL_NAME}}")
543577
544-
// Publish a message
545-
err = channel.Publish(context.Background(), "example", "message data")
578+
unsubscribe, err := channel.Subscribe(context.Background(), func(msg *ably.Message) {
579+
fmt.Printf("Received message: %v\n", msg.Data)
580+
})
546581
if err != nil {
547-
log.Fatalf("Failed to publish message: %v", err)
582+
log.Fatalf("Failed to subscribe: %v", err)
548583
}
584+
defer unsubscribe()
549585
550-
// Attach the channel
551-
_ = channel.Attach(context.Background())
586+
err = channel.Attach(context.Background())
587+
if err != nil {
588+
log.Fatalf("Failed to attach: %v", err)
589+
}
552590
553-
// Get channel history until attach
554591
history, _ := channel.HistoryUntilAttach()
555592
pages, _ := history.Pages(context.Background())
556593
for pages.Next(context.Background()) {
557594
for _, message := range pages.Items() {
558-
fmt.Println(message)
595+
fmt.Printf("Last message before attach: %v\n", message.Data)
559596
}
560597
}
561598
```
@@ -568,7 +605,12 @@ final realtime = ably.Realtime(options: clientOptions);
568605
final channel = realtime.channels.get(
569606
'{{RANDOM_CHANNEL_NAME}}'
570607
);
571-
await channel.publish(name: 'example', data: 'message data');
608+
609+
final channelMessageSubscription = channel.subscribe()
610+
.listen((message) {
611+
print('Received message: ${message.data}');
612+
});
613+
572614
await channel.attach();
573615
574616
final historyParams = ably.RealtimeHistoryParams(untilAttach: true);

0 commit comments

Comments
 (0)