matrix-dotnet-sdk
matrix-dotnet-sdk copied to clipboard
This open-source library allows you to build .NET apps compatible with Matrix Protocol
Matrix .NET SDK
This open-source library allows you to build .NET apps compatible with Matrix Protocol. It has support for a limited subset of the APIs presently.
This SDK was built for interaction with the Beacon Node. It supports login through the crypto_auth_provider.py
.
Supported Platforms
- .NET Standard 2.0 or greater
Installation
Matrix .NET SDK is available on NuGet:
dotnet add package Matrix.Sdk
Use the SDK in your app
For a complete example, refer to Sample.cs
.
You can also clone this repository and run Matrix.Sdk.Sample.Console
.
Here is step by step guide:
1. Create
Use MatrixClientFactory
to create an instance of MatrixClient
var factory = new MatrixClientFactory();
IMatrixClient client = factory.Create();
2. Login
Currently, MatrixClient
supports only password login.
await client.LoginAsync(matrixNodeAddress, username, password, deviceId);
3. Start listening for incoming events
To listen for the incoming Matrix room events you need to subscribe to OnMatrixRoomEventsReceived;
client.OnMatrixRoomEventsReceived += (sender, eventArgs) =>
{
foreach (BaseRoomEvent roomEvent in eventArgs.MatrixRoomEvents)
{
if (roomEvent is not TextMessageEvent textMessageEvent)
continue;
(string roomId, string senderUserId, string message) = textMessageEvent;
if (client.UserId != senderUserId)
Console.WriteLine($"RoomId: {roomId} received message from {senderUserId}: {message}.");
}
};
I recommend that you don't use anonymous functions to subscribe to events if you have to unsubscribe from the event at some later point in your code.
Then you should start MatrixClient
client.Start();
If you need to stop listening, for example, when the app is suspended, then do the following
client.Stop();
4. Basic functions
// Create room
CreateRoomResponse createRoomResponse = await client.CreateTrustedPrivateRoomAsync(new[]
{
anotherClient.UserId
});
// Join room
await anotherClient.JoinTrustedPrivateRoomAsync(createRoomResponse.RoomId);
// Send message
await client.SendMessageAsync(createRoomResponse.RoomId, "some message");
// Get joined rooms ids
await client.GetJoinedRoomsIdsAsync();
// Leave room
await client.LeaveRoomAsync(roomId);
5. Status check
IsLoggedIn
- matrix login status.
IsSyncing
- sync. Read more about syncing here.
Console.WriteLine($"client.IsLoggedIn: {client.IsLoggedIn}");
Console.WriteLine($"client.IsSyncing: {client.IsSyncing}");