Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why was it sent to the client twice? #139

Open
GoneGo1ng opened this issue Jan 14, 2021 · 2 comments
Open

Why was it sent to the client twice? #139

GoneGo1ng opened this issue Jan 14, 2021 · 2 comments
Labels
kind: investigate type: bug Something isn't working
Milestone

Comments

@GoneGo1ng
Copy link

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)
						}
					}
				}
			}
		}
	}
}
@GoneGo1ng
Copy link
Author

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

@Clivern
Copy link
Owner

Clivern commented Jan 18, 2021

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

@Clivern Clivern added kind: investigate type: bug Something isn't working labels Jan 18, 2021
@Clivern Clivern added this to the 2.0.0 milestone Jan 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: investigate type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants