Accessing Realm Features with Unity and C#

Hello there!
We are currently developing an app with C# via Unity, using the MongoDB C#/.NET Driver. Working with the database itself works flawless (insert, update, read, etc.), but we have problems to access the Mongo DB Realm features. We’re trying to call serverside functions, but had no success. I see that C#/Unity is not officially supported by Realm, but want to ask if there are some known solutions or workarounds. My current solution approach is to my write my code in Javascript (which itself worked, tested via Node.js), and then try to access this JS methods from C#. This should be possible theoretically, but is somewhat difficult, since Unity does not longer support Javascript. Has anyone experiences or suggestions? Thank you in advance!

Hi @Felix_Reichel!

Since Realm natively implements a subset of the MongoDB Wire Protocol, it might be possible to configure your app to connect to Realm using that and the .NET driver.

Check out Connect Over the Wire Protocol for more details.

But the TL;DR of it is:

First, Enable the wire protocol connections in Realm

Then, you’d connect to Realm via connection string, using the driver. What’s key here is the appName parameter as that’s how you’d tie it to your Realm app:

// Sample Connection String (not all options may be used by you)
var client = new MongoClient("mongodb://<user>:<password>@realm.mongodb.com:27020/?authMechanism=PLAIN&authSource=%24external&ssl=true&appName=realm-application-abcde:mongodb-atlas:local-userpass");

And the call a function:

// Set your Realm function and any arguments 
var command = new BsonDocument { { callFunction: "getEmployeeById", arguments: ["5ae782e48f25b9dc5c51c4a5"] } };

var mongoDatabase = mongoClient.GetDatabase("database");
var result = mongoDatabase.RunCommand<BsonDocument>(command);

Hope this helps and let me know if this works (or doesn’t and we can investigate further :blush:)!

1 Like

@Felix_Reichel Realm .NET SDK now supports Unity - https://www.mongodb.com/how-to/getting-started-realm-sdk-unity/

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.