I changed you code only slightly (Name instead of address and logged name with create and update).
These messages were logged:
***** Websocket message: Received message $create
***** Created Alesdair the Blessed
***** Websocket message: Result received.
***** Websocket message: Received message $create
***** Created Bob
***** Websocket message: Listening, status: Open
***** Websocket message: Received message $update
***** Websocket message: Listening, status: Open
***** Updated Alesdair the Blessed
***** Websocket message: Listening, status: Open
***** Websocket message: Result received.
***** Websocket message: Received message $update
***** Websocket message: Listening, status: Open
***** Updated Bob
***** Websocket message: Result received.
***** Websocket message: Listening, status: Open
***** Websocket message: Result received.
***** Websocket message: Received message $delete
***** Websocket message: Listening, status: Open
***** Deleted
***** Websocket message: Result received.
***** Websocket message: Listening, status: Open
***** Websocket message: Result received.
***** Websocket message: Received message $delete
***** Deleted
Here is the whole class I used:
using Assets.Scripts;
using MoralisWeb3ApiSdk;
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Threading;
#if UNITY_WEBGL
using Cysharp.Threading.Tasks;
using Moralis;
using Moralis.WebGL.Platform.Objects;
using Moralis.WebGL.Platform.Queries;
using Moralis.WebGL.Platform.Queries.Live;
#else
using System.Threading.Tasks;
using Moralis;
using Moralis.Platform.Objects;
using Moralis.Platform.Queries;
using Moralis.Platform.Queries.Live;
#endif
public class PlayerData : MoralisObject
{
public long TokenCount { get; set; }
public string Name { get; set; }
public PlayerData() : base("PlayerData") { }
}
public class LiveQueryTest
{
#if UNITY_WEBGL
public async static UniTask TestLiveQuery()
{
MoralisQuery<PlayerData> query = await MoralisInterface.GetClient().Query<PlayerData>();
// Setup subscription
SetupLiveQuerySubscription(query);
Thread.Sleep(2000);
#else
public async static Task TestLiveQuery()
{
MoralisQuery<PlayerData> query = MoralisInterface.GetClient().Query<PlayerData>();
// Setup subscription
SetupLiveQuerySubscription(query);
Thread.Sleep(2000);
#endif
// Setup subscription
SetupLiveQuerySubscription(query);
Thread.Sleep(2000);
System.Random rand = new System.Random((int)DateTime.Now.Ticks);
int x = rand.Next(25) + 3;
// Create some data
PlayerData p1 = MoralisInterface.GetClient().Create<PlayerData>();
p1.Name = GetTestName();
p1.TokenCount = x;
await p1.SaveAsync();
x = rand.Next(25) + 3;
PlayerData p2 = MoralisInterface.GetClient().Create<PlayerData>();
p2.Name = GetTestName();
p2.TokenCount = x;
await p2.SaveAsync();
// Get the records created
IEnumerable<PlayerData> recs = await query.FindAsync();
// Update dtat
foreach (PlayerData pd in recs)
{
x = rand.Next(25) + 3;
pd.TokenCount = x;
await pd.SaveAsync();
}
// Delete data
foreach (PlayerData pd in recs)
{
await pd.DeleteAsync();
}
}
private static void SetupLiveQuerySubscription(MoralisQuery<PlayerData> query)
{
MoralisLiveQueryCallbacks<PlayerData> callbacks = new MoralisLiveQueryCallbacks<PlayerData>();
callbacks.OnConnectedEvent += (() => { Console.WriteLine("Connection Established."); });
callbacks.OnSubscribedEvent += ((requestId) => { Console.WriteLine($"Subscription {requestId} created."); });
callbacks.OnUnsubscribedEvent += ((requestId) => { Console.WriteLine($"Unsubscribed from {requestId}."); });
callbacks.OnErrorEvent += ((ErrorMessage em) =>
{
Debug.Log($"***** ERROR: code: {em.code}, msg: {em.error}, requestId: {em.requestId}");
});
callbacks.OnCreateEvent += ((item, requestId) =>
{
Debug.Log($"***** Created {item.Name}");
});
callbacks.OnUpdateEvent += ((item, requestId) =>
{
Debug.Log($"***** Updated {item.Name}");
});
callbacks.OnDeleteEvent += ((item, requestId) =>
{
Debug.Log($"***** Deleted");
});
callbacks.OnGeneralMessageEvent += ((text) =>
{
Debug.Log($"***** Websocket message: {text}");
});
MoralisLiveQueryController.AddSubscription<PlayerData>("PlayerData", query, callbacks);
}
private static string GetTestName()
{
string[] names = { "Clem the Great", "Sion the Bold", "Bob", "D@ve", "Oogmar the Deft", "Alesdair the Blessed", "Seviel the Mighty", "Master Adept Xactant", "Semaphore the Beautiful", "Gamemaster Nexnang" };
System.Random rand = new System.Random((int)DateTime.Now.Ticks);
int x = rand.Next(names.Length);
x = rand.Next(names.Length);
return names[x];
}
}
Note, I only tested this in desktop.