A .Net client library for www.ably.io, the realtime messaging service.
For complete API documentation, see the ably documentation.
Visit https://www.ably.io/documentation for a complete API reference and more examples.
The client library is available as a nuget package.
You can install it from the Package Manager Console using this command
PM> Install-Package ably.io
All examples assume a client has been created as follows:
// using basic auth with API key
var realtime = new AblyRealtime("<api key>");
// using taken auth with token string
var realtime = new AblyRealtime(new ClientOptions { Token = "token" });
If you do not have an API key, sign up for a free API key now
Connecting and observing connection state changes. By default the library automatically initialises a connection.
realtime.Connection.On(ConnectionState.Connected, args =>
{
//Do stuff
});
To disable the default automatic connect behaviour of the library, set AutoConnect=false
when initialising the client.
var realtime = new AblyRealtime(new ClientOptions("<api key>") {AutoConnect = false});
// some code
realtime.Connect();
Subscribing to connection state changes and observing errors:
realtime.Connection.On(args =>
{
var currentState = args.Current; //Current state the connection transitioned to
var previousState = args.Previous; // Previous state
var error = args.Reason; // If the connection errored the Reason object will be populated.
});
Create a channel
var channel = realtime.Channels.Get("test");
Subscribing to all events:
channel.Subscribe(message =>
{
var name = message.Name;
var data = message.Data;
});
Subscribing to specific events:
channel.Subscribe("myEvent", message =>
{
var name = message.Name;
var data = message.Data;
});
Observing channel state changes and errors:
channel.On(args =>
{
var state = args.NewState; //Current channel State
var error = args.Error; // If the channel errored it will be refrected here
});
//or
channel.On(ChannelState.Attached, args =>
{
// Do stuff when channel is attached
});
The client support a callback and async publishing. The simplest way to publish is:
channel.Publish("greeting", "Hello World!");
with a callback:
channel.Publish("greeting", "Hello World!", (success, error) =>
{
//if publish succeeded `success` is true
//if publish failed `success` is false and error will contain the specific error
});
and the async version which if you await
it will complete when the message has been acknowledged or rejected by the Ably service:
var result = await channel.PublishAsync("greeting", "Hello World!");
//You can check if the message failed
if (result.IsFailure)
{
var error = result.Error; // The error reason can be accessed as well
}
Calling history returns a paginated list of message. The object is of type PaginatedResult<Message>
and can be iterated through as a normal list.
var history = await channel.HistoryAsync();
//loop through current history page
foreach (var message in history.Items)
{
//Do something with message
}
//Get next page.
var nextPage = await history.NextAsync();
Getting presence history is similar to how message history works. You get back PaginatedResult<PresenceMessage>
and can navigate or iterate through the page
var presenceHistory = await channel.Presence.HistoryAsync();
//loop through the presence messages
foreach (var presence in presenceHistory.Items)
{
//Do something with the messages
}
var presenceNextPage = await presenceHistory.NextAsync();
When a 128 bit or 256 bit key is provided to the library, all payloads are encrypted and decrypted automatically using that key on the channel. The secret key is never transmitted to Ably and thus it is the developer's responsibility to distribute a secret key to both publishers and subscribers.
var secret = Crypto.GetRandomKey();
var encryptedChannel = realtime.Get("encrypted", new ChannelOptions(secret));
encryptedChannel.Subscribe(message =>
{
var data = message.data; // sensitive data (encrypted before published)
});
encryptedChannel.Publish("name (not encrypted)", "sensitive data (encrypted before published)");
The rest client provides a fully async wrapper around the Ably service web api.
All examples assume a client and/or channel has been created as follows:
var client = new AblyRest("<api key>");
var channel = client.Channels.Get("test");
If you do not have an API key, sign up for a free API key now
await channel.PublishAsync("name", "data");
If the publish is not successful an error will be thrown of type AblyException
containing error codes and error description
try
{
await channel.PublishAsync("name", "errorData");
}
catch(AblyException ablyError)
{
// Log error
}
var historyPage = await channel.HistoryAsync();
foreach (var message in historyPage.Items)
{
//Do something with each message
}
//get next page
var nextHistoryPage = await historyPage.NextAsync();
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
}
Tokens are issued by Ably and are readily usable by any client to connect to Ably:
var token = await client.Auth.RequestTokenAsync();
var tokenString = token.Token; // "xVLyHw.CLchevH3hF....MDh9ZC_Q"
var tokenClient = new AblyRest(new ClientOptions { TokenDetails = token });
Token requests are issued by your servers and signed using your private API key. This is the preferred method of authentication as no secrets are ever shared, and the token request can be issued to trusted clients without communicating with Ably.
string tokenRequest = await client.Auth.CreateTokenRequestAsync();
When a 128 bit or 256 bit key is provided to the library, all payloads are encrypted and decrypted automatically using that key on the channel. The secret key is never transmitted to Ably and thus it is the developer's responsibility to distribute a secret key to both publishers and subscribers.
var secret = Crypto.GetRandomKey();
var 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.First().data; // "sensitive data" the message will be automatically decrypted once received
var stats = await client.StatsAsync();
var firstItem = stats.Items.First();
var nextStatsPage = await stats.NextAsync();
DateTimeOffset time = await client.TimeAsync();
This library has dependencies that can differ depending on the target platform. See the nuget page for specifics.
- Xamarin iOS and Android
- .Net 4.6+ *
- .Net Standard 1.4+
- Mono
- UWP
NOTE: To target Windows 7 a custom ITransportFactory will need to be implemented in your project that uses an alternate Web Socket library. This is because System.Net.WebSockets is not fully implementented on Windows 7. See this repository for a working example using the websocket4net library.
Please visit http://support.ably.io/ for access to our knowledgebase and to ask for any assistance.
You can also view the community reported Github issues.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Ensure you have added suitable tests and the test suite is passing
- Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The build scripts are written in powershell using PSake and need to be run on Windows with Visual Studio 2017 installed. Additionally nuget.exe and GitVersion.exe are required, these can be installed via chocolatey
choco install nuget.commandline
choco install gitversion.portable
Running .\build.ps1
will start the build process and run the tests.
Running package.ps1
will run the build script and create a nuget package.
This library uses semantic versioning. For each release, the following needs to be done:
- Update the version number in GitVersion.yml* and commit the change.
- Run
github_changelog_generator
to automate the update of the CHANGELOG. Once theCHANGELOG
update has completed, manually change theUnreleased
heading and link with the current version number such asv1.0.0
. Also ensure that theFull Changelog
link points to the new version tag instead of theHEAD
. Commit this change. - Add a tag and push to origin such as
git tag v1.0.0 && git push origin v1.0.0
- Visit https://github.com/ably/ably-dotnet/tags and
Add release notes
for the release including links to the changelog entry. - Run
package.ps1
to create the nuget package. - Run
nuget push ably.io.*.nupkg -Source https://www.nuget.org/api/v2/package
(a private nuget API Key is required to complete this step, more information on publishing nuget packages can be found here)
*GitVersion is required, see the preceeding section 'Building and Packaging' for more information.
Copyright (c) 2016 Ably Real-time Ltd, Licensed under the Apache License, Version 2.0. Refer to LICENSE for the license terms.
A .Net client library for www.ably.io, the realtime messaging service.
For complete API documentation, see the ably documentation.
Visit https://www.ably.io/documentation for a complete API reference and more examples.
The client library is available as a nuget package.
You can install it from the Package Manager Console using this command
PM> Install-Package ably.io
All examples assume a client has been created as follows:
// using basic auth with API key
var realtime = new AblyRealtime("<api key>");
// using taken auth with token string
var realtime = new AblyRealtime(new ClientOptions { Token = "token" });
If you do not have an API key, sign up for a free API key now
Connecting and observing connection state changes. By default the library automatically initialises a connection.
realtime.Connection.On(ConnectionState.Connected, args =>
{
//Do stuff
});
To disable the default automatic connect behaviour of the library, set AutoConnect=false
when initialising the client.
var realtime = new AblyRealtime(new ClientOptions("<api key>") {AutoConnect = false});
// some code
realtime.Connect();
Subscribing to connection state changes and observing errors:
realtime.Connection.On(args =>
{
var currentState = args.Current; //Current state the connection transitioned to
var previousState = args.Previous; // Previous state
var error = args.Reason; // If the connection errored the Reason object will be populated.
});
Create a channel
var channel = realtime.Channels.Get("test");
Subscribing to all events:
channel.Subscribe(message =>
{
var name = message.Name;
var data = message.Data;
});
Subscribing to specific events:
channel.Subscribe("myEvent", message =>
{
var name = message.Name;
var data = message.Data;
});
Observing channel state changes and errors:
channel.On(args =>
{
var state = args.NewState; //Current channel State
var error = args.Error; // If the channel errored it will be refrected here
});
//or
channel.On(ChannelState.Attached, args =>
{
// Do stuff when channel is attached
});
The client support a callback and async publishing. The simplest way to publish is:
channel.Publish("greeting", "Hello World!");
with a callback:
channel.Publish("greeting", "Hello World!", (success, error) =>
{
//if publish succeeded `success` is true
//if publish failed `success` is false and error will contain the specific error
});
and the async version which if you await
it will complete when the message has been acknowledged or rejected by the Ably service:
var result = await channel.PublishAsync("greeting", "Hello World!");
//You can check if the message failed
if (result.IsFailure)
{
var error = result.Error; // The error reason can be accessed as well
}
Calling history returns a paginated list of message. The object is of type PaginatedResult<Message>
and can be iterated through as a normal list.
var history = await channel.HistoryAsync();
//loop through current history page
foreach (var message in history.Items)
{
//Do something with message
}
//Get next page.
var nextPage = await history.NextAsync();
Getting presence history is similar to how message history works. You get back PaginatedResult<PresenceMessage>
and can navigate or iterate through the page
var presenceHistory = await channel.Presence.HistoryAsync();
//loop through the presence messages
foreach (var presence in presenceHistory.Items)
{
//Do something with the messages
}
var presenceNextPage = await presenceHistory.NextAsync();
When a 128 bit or 256 bit key is provided to the library, all payloads are encrypted and decrypted automatically using that key on the channel. The secret key is never transmitted to Ably and thus it is the developer's responsibility to distribute a secret key to both publishers and subscribers.
var secret = Crypto.GetRandomKey();
var encryptedChannel = realtime.Get("encrypted", new ChannelOptions(secret));
encryptedChannel.Subscribe(message =>
{
var data = message.data; // sensitive data (encrypted before published)
});
encryptedChannel.Publish("name (not encrypted)", "sensitive data (encrypted before published)");
The rest client provides a fully async wrapper around the Ably service web api.
All examples assume a client and/or channel has been created as follows:
var client = new AblyRest("<api key>");
var channel = client.Channels.Get("test");
If you do not have an API key, sign up for a free API key now
await channel.PublishAsync("name", "data");
If the publish is not successful an error will be thrown of type AblyException
containing error codes and error description
try
{
await channel.PublishAsync("name", "errorData");
}
catch(AblyException ablyError)
{
// Log error
}
var historyPage = await channel.HistoryAsync();
foreach (var message in historyPage.Items)
{
//Do something with each message
}
//get next page
var nextHistoryPage = await historyPage.NextAsync();
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
}
Tokens are issued by Ably and are readily usable by any client to connect to Ably:
var token = await client.Auth.RequestTokenAsync();
var tokenString = token.Token; // "xVLyHw.CLchevH3hF....MDh9ZC_Q"
var tokenClient = new AblyRest(new ClientOptions { TokenDetails = token });
Token requests are issued by your servers and signed using your private API key. This is the preferred method of authentication as no secrets are ever shared, and the token request can be issued to trusted clients without communicating with Ably.
string tokenRequest = await client.Auth.CreateTokenRequestAsync();
When a 128 bit or 256 bit key is provided to the library, all payloads are encrypted and decrypted automatically using that key on the channel. The secret key is never transmitted to Ably and thus it is the developer's responsibility to distribute a secret key to both publishers and subscribers.
var secret = Crypto.GetRandomKey();
var 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.First().data; // "sensitive data" the message will be automatically decrypted once received
var stats = await client.StatsAsync();
var firstItem = stats.Items.First();
var nextStatsPage = await stats.NextAsync();
DateTimeOffset time = await client.TimeAsync();
Currently a portable (PCL) version is not available.
Please visit http://support.ably.io/ for access to our knowledgebase and to ask for any assistance.
You can also view the community reported Github issues.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Ensure you have added suitable tests and the test suite is passing
- Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The build scripts are written in powershell using PSake and need to be run on Windows with Visual Studio 2017 installed. Additionally nuget.exe and GitVersion.exe are required, these can be installed via chocolatey
choco install nuget.commandline
choco install gitversion.portable
Running .\build.ps1
will start the build process and run the tests.
Running package.ps1
will run the build script and create a nuget package.
This library uses semantic versioning. For each release, the following needs to be done:
- Update the version number in GitVersion.yml* and commit the change.
- Run
github_changelog_generator
to automate the update of the CHANGELOG. Once theCHANGELOG
update has completed, manually change theUnreleased
heading and link with the current version number such asv1.0.0
. Also ensure that theFull Changelog
link points to the new version tag instead of theHEAD
. Commit this change. - Add a tag for the version and push to origin such as
git tag 1.0.0 && git push origin 1.0.0
. For beta versions the version string should beMaj.Min.Patch-betaN
, e.g1.0.0-beta1
- Visit https://github.com/ably/ably-dotnet/tags and
Add release notes
for the release including links to the changelog entry. - Run
package.ps1
to create the nuget package. - Run
nuget push ably.io.*.nupkg -Source https://www.nuget.org/api/v2/package
(a private nuget API Key is required to complete this step, more information on publishing nuget packages can be found here)
*GitVersion is required, see the proceeding section 'Building and Packaging' for more information.
Copyright (c) 2016 Ably Real-time Ltd, Licensed under the Apache License, Version 2.0. Refer to LICENSE for the license terms.