public async Task Send20MessagesAndThenPaginateHistorySync(Protocol protocol) { //Arrange var client = await GetRestClient(protocol); IRestChannel channel = client.Channels.Get("persisted:historyTest:" + protocol); //Act for (int i = 0; i < 20; i++) { channel.Publish("name" + i, "data" + i); } //Assert var history = channel.History(new HistoryRequestParams() { Limit = 10 }); history.Items.Should().HaveCount(10); history.HasNext.Should().BeTrue(); history.Items.First().Name.Should().Be("name19"); var secondPage = history.Next(); secondPage.Items.Should().HaveCount(10); secondPage.Items.First().Name.Should().Be("name9"); }
//Uses the to publish the examples inside crypto-data-256.json to publish and then retrieve the messages public async Task CanPublishAMessageAndRetrieveIt256(Protocol protocol) { var items = (JArray)examples256["items"]; AblyRest ably = await GetRestClient(protocol); IRestChannel channel = ably.Channels.Get("persisted:test".AddRandomSuffix(), GetOptions(examples256)); var count = 0; foreach (var item in items) { var encoded = item["encoded"]; var encoding = (string)encoded["encoding"]; var decodedData = DecodeData((string)encoded["data"], encoding); await channel.PublishAsync((string)encoded["name"], decodedData); var message = (await channel.HistoryAsync()).Items.First(); if (message.Data is byte[]) { (message.Data as byte[]).Should().BeEquivalentTo(decodedData as byte[], "Item number {0} data does not match decoded data", count); } else if (encoding == "json") { JToken.DeepEquals((JToken)message.Data, (JToken)decodedData).Should().BeTrue("Item number {0} data does not match decoded data", count); } else { message.Data.Should().Be(decodedData, "Item number {0} data does not match decoded data", count); } count++; } }
public static async Task Test() { ConsoleColor.DarkGreen.WriteLine("Creating sandbox app.."); ConsoleColor.DarkGreen.WriteLine("Creating REST client.."); // Create REST client using that key AblyRest ably = new AblyRest(new ClientOptions() { AuthUrl = new Uri("https://www.ably.io/ably-auth/token-request/demos"), Tls = false }); ConsoleColor.DarkGreen.WriteLine("Publishing a message.."); // Verify we can publish IRestChannel channel = ably.Channels.Get("persisted:presence_fixtures"); var tsPublish = DateTimeOffset.UtcNow; await channel.PublishAsync("test", true); ConsoleColor.DarkGreen.WriteLine("Getting the history.."); PaginatedResult <Message> history = await channel.HistoryAsync(); if (history.Items.Count <= 0) { throw new ApplicationException("Message lost: not on the history"); } Message msg = history.Items.First(); var tsNow = DateTimeOffset.UtcNow; var tsHistory = msg.Timestamp.Value; if (tsHistory < tsPublish) { throw new ApplicationException("Timestamp's too early. Please ensure your PC's time is correct, use e.g. time.nist.gov server."); } if (tsHistory > tsNow) { throw new ApplicationException("Timestamp's too late, Please ensure your PC's time is correct, use e.g. time.nist.gov server."); } ConsoleColor.DarkGreen.WriteLine("Got the history"); }
public ChannelHistory(ITestOutputHelper output) : base(output) { _client = GetRestClient(); _channel = _client.Channels.Get("test"); }
public async Task RestApiSamples() { var client = new AblyRest(PlaceholderKey); IRestChannel channel = client.Channels.Get("test"); try { await channel.PublishAsync("name", "data"); } catch (AblyException) { // Log error } // History var historyPage = await channel.HistoryAsync(); foreach (var message in historyPage.Items) { // Do something with each message } // Get next page var nextHistoryPage = await historyPage.NextAsync(); // Current presence var presence = await channel.Presence.GetAsync(); var first = presence.Items.FirstOrDefault(); var clientId = first.ClientId; // 'clientId' of the first member present var nextPresencePage = await presence.NextAsync(); foreach (var presenceMessage in nextPresencePage.Items) { // Do stuff with next page presence messages } // Presence history var presenceHistory = await channel.Presence.HistoryAsync(); foreach (var presenceMessage in presenceHistory.Items) { // Do stuff with presence messages } var nextPage = await presenceHistory.NextAsync(); foreach (var presenceMessage in nextPage.Items) { // Do stuff with next page messages } // Publishing encrypted messages var secret = Crypto.GenerateRandomKey(); IRestChannel encryptedChannel = client.Channels.Get("encryptedChannel", new ChannelOptions(secret)); await encryptedChannel.PublishAsync("name", "sensitive data"); // Data will be encrypted before publish var history = await encryptedChannel.HistoryAsync(); var data = history.Items.First().Data; // "sensitive data" the message will be automatically decrypted once received // Generate a token var token = await client.Auth.RequestTokenAsync(); var tokenString = token.Token; // "xVLyHw.CLchevH3hF....MDh9ZC_Q" var tokenClient = new AblyRest(new ClientOptions { TokenDetails = token }); var tokenRequest = await client.Auth.CreateTokenRequestAsync(); // Stats var stats = await client.StatsAsync(); var firstItem = stats.Items.First(); var nextStatsPage = await stats.NextAsync(); var time = await client.TimeAsync(); }