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
.
- .NET Standard 2.0 or greater
Matrix .NET SDK is available on NuGet:
dotnet add package Matrix.Sdk
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:
Use MatrixClientFactory
to create an instance of MatrixClient
var factory = new MatrixClientFactory();
IMatrixClient client = factory.Create();
Currently, MatrixClient
supports only password login.
await client.LoginAsync(matrixNodeAddress, username, password, deviceId);
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();
// 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);
IsLoggedIn
- matrix login status.
IsSyncing
- sync. Read more about syncing here.
Console.WriteLine($"client.IsLoggedIn: {client.IsLoggedIn}");
Console.WriteLine($"client.IsSyncing: {client.IsSyncing}");