有没有更好的方法来使用Pusher Channels的批量事件发布消息?

huangapple go评论75阅读模式
英文:

Is there a better way to publish messages using Pusher Channels' batch event?

问题

我正在尝试使用Pusher Channels从我的服务器向用户发送消息。我的API接收到一个用户列表,消息需要发送给列表中的所有用户。我不能将这些用户分组到一个单一的频道中,必须为每个用户使用一个独立的频道。这使得我的API变得很慢,因为用户列表的大小可以在1到10000之间(未来可能更多),而Pusher批处理事件只能接受大小为10的事件列表。

我正在使用.NET 6开发我的API。

我尝试使用批处理事件来尝试提高性能;我的代码类似于以下内容:

var events = new List<Event>();
// count可以在1到10000之间
for (int i = 1; i <= count; i++)
{
    // 创建示例事件列表
    events.Add(new Event
    {
        Channel = string.Format("batch-channel-{0}", i),
        EventName = "batch-event",
        Data = new
        {
            Channel = string.Format("batch-channel-{0}", i),
            Event = "batch-event",
            Message = string.Format("{0} - 示例消息", i)
        }
    });
}
var result = new List<HttpStatusCode>();
int chunkSize = 10;
int totalChunks = (int)Math.Ceiling((double)events.Count / chunkSize);
for (int i = 0; i < totalChunks; i++)
{
    var eventsChunk = events.Skip(i * chunkSize).Take(chunkSize).ToArray();
    // 发布大小为10的事件列表
    var chunkResult = await pusher.TriggerAsync(eventsChunk);
    result.Add(chunkResult.StatusCode);
}

我使用大小为10000的事件列表测试了这段代码,执行完毕大约需要6分钟。我想知道是否有什么我遗漏的地方,以及是否可以在性能上进行改进。

感谢任何帮助。谢谢。

英文:

I'm trying to send messages to my users from my server using Pusher Channels. My api receives a list of users and the message needs to be sent to all the users in the list. I can't group these users into a single channel and an individual channel has to be used for each user. This makes my api slow as the list of users can have a size of anything between 1 and 10000 (possibly more in the future), and pusher batch events can only accept a Event list of size 10.

> I'm using .net 6 for my api

I've tried using batch events to try and improve performance; my code looks something like this,

var events = new List&lt;Event&gt;();
// count can be anything between 1 and 10000
for (int i = 1; i &lt;= count; i++)
{
    // creating a sample list of events
    events.Add(new Event
    {
        Channel = string.Format(&quot;batch-channel-{0}&quot;, i),
        EventName = &quot;batch-event&quot;,
        Data = new
        {
            Channel = string.Format(&quot;batch-channel-{0}&quot;, i),
            Event = &quot;batch-event&quot;,
            Message = string.Format(&quot;{0} - sample message&quot;, i)
        {
    });
}
var result = new List&lt;HttpStatusCode&gt;();
int chunkSize = 10;
int totalChunks = (int)Math.Ceiling((double)events.Length / chunkSize);
for (int i = 0; i &lt; totalChunks; i++)
{
    var eventsChunk = events.Skip(i * chunkSize).Take(chunkSize).ToArray();
    // publishing event lists of size 10
    var chunkResult = await pusher.TriggerAsync(eventsChunk);
    result.Add(chunkResult.StatusCode);
}

I tested this code with a Event list of size 10000 and it takes around 6 minutes to complete execution. I want to know if there is anything I'm missing and if I can somehow improve performance.

Any help is appreciated. Thank you.

答案1

得分: 1

如果您要将相同的事件发送到多个频道,那么您可以使用标准的触发端点,但指定要广播到的频道列表。例如:

using PusherServer;

var options = new PusherOptions();
options.Cluster = "APP_CLUSTER";
var pusher = new Pusher("APP_ID", "APP_KEY", "APP_SECRET", options);

ITriggerResult result = await pusher.TriggerAsync(
  new string[]{"my-channel-1", "my-channel-2", "my-channel-3"},
  "my-event",
  new { message: "hello world" });

这将触发事件到指定的三个频道。您可以在单次发布中指定最多100个频道。

如果您要向每个频道发送不同的事件,那么您提到的批量事件端点将是前进的方式。在这种情况下,您可能需要考虑多线程或类似的方法来同时处理多个批量触发,而不是顺序处理。

来源 - https://pusher.com/docs/channels/server_api/http-api/#example-publish-an-event-on-multiple-channels

英文:

If you are sending the same event to multiple channels then you can use the standard trigger endpoint but specify a list of the channels that you are broadcasting to. For example:

using PusherServer;

var options = new PusherOptions();
options.Cluster = &quot;APP_CLUSTER&quot;;
var pusher = new Pusher(&quot;APP_ID&quot;, &quot;APP_KEY&quot;, &quot;APP_SECRET&quot;, options);

ITriggerResult result = await pusher.TriggerAsync(
  new string[]{&quot;my-channel-1&quot;, &quot;my-channel-2&quot;, &quot;my-channel-3&quot;},
  &quot;my-event&quot;,
  new { message: &quot;hello world&quot; });

This would trigger the event to the three specified channels. You can specify up to 100 channels in a single publish.

If you are sending a different event to each channel then the batch event endpoint you have mentioned would be the way forward. In this case you might look at multi-threading to or similar to be able to handle multiple batch triggers at the same time, rather than sequentially.

Source - https://pusher.com/docs/channels/server_api/http-api/#example-publish-an-event-on-multiple-channels

huangapple
  • 本文由 发表于 2023年6月1日 22:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定