1

The following code snippet has been taken from this Microsoft document:

[Function("SignalRFunction")]
[SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnectionString")]
public static MyMessage Run([SignalRTrigger("SignalRTest", "messages", "SendMessage", parameterNames: new string[] { "message" },
    ConnectionStringSetting = "SignalRConnectionString")] string item,
    [SignalRConnectionInfoInput(HubName = "chat")] MyConnectionInfo connectionInfo,
    FunctionContext context)
{
    var logger = context.GetLogger("SignalRFunction");

    logger.LogInformation(item);
    logger.LogInformation($"Connection URL = {connectionInfo.Url}");

    var message = $"Output message created at {DateTime.Now}";

    return new MyMessage()
    {
        Target = "newMessage",
        Arguments = new[] { message }
    };
}

public class MyMessage
{
    public string Target { get; set; }

    public object[] Arguments { get; set; }
}

What I'd like to do is to return multiple instances of MyMessage simultaneously to send a message to different targets. Also, I'd like to return a JSON payload from Run function comprising some other info e.g. a database record, etc.

How can the Run function be augmented to support such a case?

Arash
  • 3,628
  • 5
  • 46
  • 70
  • You can just return a list of objects you just have to change the return type and if you want to send the objects to other azure services such as event hub, blob storage etc. you can create their individual client to send that data – Mohit Ganorkar Jan 05 '23 at 10:44
  • @MohitGanorkar Thanks for the reply. Could you please lead me to a C# example? – Arash Jan 05 '23 at 13:19

1 Answers1

0
  • You can return multiple instances of a class as a list of objects. For that you will have to change the data type of the Run to the list of objects.

  • Here I have used an HTTP trigger, but the concept should also work of SignalIr trigger as well. Where we would add List<TestCLass> as a data type of the Run function where TestClass is the class those instances we are returning.


public class TestClass
    {
        public string Target { get; set; }
  }  

public static List<TestClass> Run()
{
            List<TestClass> tests = new List<TestClass>();
            TestClass t = new TestClass();
            TestClass q = new TestClass();
            t.Target = "Hello World";
            q.Target = "Another Hello World";
            tests.Add(t);
            tests.Add(q);
            return tests;
}

output of the HTTP trigger: enter image description here

  • Also, If you want to connect to other for e.g.: azure service Bus then you will have to create a client for that particular service. for e.g.: Here in this MSDOC the ServieBusClient to connect to azure service Bus. Similarly, you can find clients for other azure services as well
Mohit Ganorkar
  • 1,917
  • 2
  • 6
  • 11
  • @MohitGanokar Thanks. So, if I understood correctly, if I want to have the `Run` function which is a signalR trigger function add a user to a group of users AND send a message to the user, I should return a list of objects whose elements are SignalRGroupAction and SignalRMessageAction. Is this a correct grasp? – Arash Jan 06 '23 at 13:04