Giter VIP home page Giter VIP logo

matrix-dotnet-sdk's Introduction

Matrix .NET SDK

matrix-logo

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

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}");

matrix-dotnet-sdk's People

Contributors

iviich4el avatar k-karuna avatar m-kus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.