Clivern / Beaver

💨 A real time messaging system to build a scalable in-app notifications, multiplayer games, chat apps in web and mobile apps.

Home Page:https://clivern.github.io/Beaver/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why was it sent to the client twice?

GoneGo1ng opened this issue · comments

When I set the "to_client" parameter, the client will receive two duplicate messages.
Why is it designed like this?

// HandleMessages send messages to a specific connected client
func (e *Websocket) HandleMessages() {

	validate := utils.Validator{}

	for {
		// Grab the next message from the broadcast channel
		msg := <-e.Broadcast

		// Send to Client
		if msg.IsValid() && !validate.IsEmpty(msg.ToClient) && !validate.IsEmpty(msg.Channel) && validate.IsUUID4(msg.ToClient) {
			// Push message to that client if it still connected
			// or remove from clients if we can't deliver messages to
			// it anymore
			if client, ok := e.Clients.Get(msg.ToClient); ok {
// --------------------------------------------First--------------------------------------------------
				err := client.(*websocket.Conn).WriteJSON(msg)
				if err != nil {
					client.(*websocket.Conn).Close()
					e.Clients.Delete(msg.ToClient)
				}
			}
		}

		// Send to client Peers on a channel
		if msg.IsValid() && !validate.IsEmpty(msg.FromClient) && !validate.IsEmpty(msg.Channel) && validate.IsUUID4(msg.FromClient) {

			channel := api.Channel{}
			channel.Init()
			iter := channel.ChannelScan(msg.Channel).Iterator()

			for iter.Next() {

				if msg.FromClient == iter.Val() {
					continue
				}

				msg.ToClient = iter.Val()

				if msg.ToClient != "" && validate.IsUUID4(msg.ToClient) {
					if client, ok := e.Clients.Get(msg.ToClient); ok {
// --------------------------------------------Second--------------------------------------------------
						err := client.(*websocket.Conn).WriteJSON(msg)
						if err != nil {
							client.(*websocket.Conn).Close()
							e.Clients.Delete(msg.ToClient)
						}
					}
				}
			}
		}
	}
}

And what are the differences between different channelTypes("public", "private", "presence")?

commented

That must be a bug then, will check while working on v2 & backport to master

Regarding your question

  • Public channels should be used for publicly accessible data as they do not require any form authorisation in order to be subscribed to.

  • Private channels should be used when access to the channel needs to be restricted in some way.

  • Presence channels build on the security of private channels and expose the additional feature of an awareness of who is subscribed to that channel. It can be used to add "who's online" type of functionality to your application