When to broadcast an event and when to use a peer fiber

Options
Hey guys,

I'm struggling to get my head around when I should be using the Application.broadcast method against the PeerBase.sendEvent method. I understand that broadcasting avoids a peers fiber, but I'm struggling to work out when I would actually need to use the peers fiber at all. Im currently considering having all events broadcast, and handle the pooling/timing separately.

Any advice would be greatly appreciated.

Comments

  • Trixer
    Options
    I would almost never use fiber if you can avoid it. Its very low level and unless you really know what you are doing on the lower end of photon, it might just mess you up.

    Id stick to events and operations depending on your need.
  • chvetsov
    Options
    Hi, there.

    Either you use broadcast or Peer.SendEvent there is nothing to do with fiber. you may use SendEvent as you wish, it is threadsafe. You will need it only when you need to send something to this peer only. if there is no such need, do not use it.

    Nothing scarry will happen if you will use Peer.RequestFiber for some synchronized access. but always keep in mind that if your operation will be too long (too long depends on your needs) this will deley operation handling.
  • @chvetsov - thanks for the reply.

    My understanding is that Peer.SendEvent() method queues the event on the Peer.RequestFiber, while the ApplicationBase.Broadcast() method doesn't call Peer.SendEvent() on each peer, but sends the message directly (bypassing the Peer.RequestFiber).

    So to clarify, what I currently understand:
    - Peer.SendOpertationResponse() and Peer.SendEvent() queues the message on the Peer.RequestFiber
    - ApplicationBase.Broadcast() sends the message directly to all peers (bypassing the Peer.RequestFiber)

    I've noticed a fairly large difference in performance between the two, in that for high-frequency messages (synchronization updates for example), broadcasting needs to be used, otherwise the queue on the fiber grows and grows and messages aren't being sent out quick enough. But this is about the closest thing I've come to as a conclusion so far: that low frequency messages should go on the fiber while high frequency messages should be broadcasted... but i'm not quite sure if this is correct?

    Ideally, i'd love to be broadcasting every event and operation response, because then I can be more confident that messages are getting to peers as quickly as possible. Provided that I am making sure messages are going to peers in the correct order, this should be okay?


    Also, you mention that Peer.SendEvent() is thread safe, what exactly is the problem that Peer.SendEvent() resolves in terms of thread safety that ApplicationBase.Broadcast() doesn't? Is there some shared resource that I should be concerned about? I'd love some clarification on this.

    As always, thanks muchly for the help and advice!
  • chvetsov
    Options
    your understanding is not entirly correct.

    Neither SendEvent nor SendOperationResponse enqueue anything in fiber. they all send data directly. all SendEvent, SendOperationResponse and ApplicationBase.BroadcastEvent are thread safe. The only one concern about ApplicationBase.BroadcastEvent is list of peers. it should not be modified.

    Main difference between use of BroadcastEvent and SendEvent is that data serialized once in case of BroacastEvent. That is why it is more performant. You may use it all the times except case when you need to send data to exactly one client. But even in this case it might be used. so, for instance if your enumerator will contain only one peer there is nothing wrong with use of BroadcastEvent.

    SendOperationResponse is supposed to be used for response to operation request. This means that it can be used only to send response to one client. To client who sent operation request.

    hope now it is more clear for you
  • @chvetsov - thank you for clearing this up for me!

    This has really helped me to understand things a great deal. So by the looks of things i'll be broadcasting all of my events going forward!

    Thanks again.