How to sending Notifications to user connections with condition?

Hello Nakama Community,

I wanted to share a feature I’ve implemented in Nakama, and I believe it could be a valuable addition to the platform. Currently, Nakama primarily handles notifications at the user level, but I’ve created a custom solution that allows sending notifications to user connections (sockets) of the same user with a condition.

Feature Overview:

  • Enable sending notifications to specific user connections.
  • Maintain a mapping of user IDs to connections, allowing for precise targeting.
  • Implemented as a custom solution, providing flexibility to Nakama users.

Use Cases:

  • User connects multiple sockets or a server with multiple games. When I send a message to one game, not send it to another game (the same user, one user connects two games, which are managed by one server at the same time).

How It Works:

  • Send Notifications func with a callback func return bool, if true send noti to session
func (r *LocalMessageRouter) SendToStreamCondition(logger *zap.Logger, stream PresenceStream, envelope *rtapi.Envelope, reliable bool, isOk func(vars map[string]string) bool) {

	if isOk == nil {
		r.SendToStream(logger, stream, envelope, reliable)
		return
	}

	presenceIDs := []*PresenceID{}
	for _, p := range r.tracker.ListPresenceIDByStream(stream) {
		s := r.sessionRegistry.Get(p.SessionID)
		if isOk(s.Vars()) {
			presenceIDs = append(presenceIDs, p)
		}
	}

	r.SendToPresenceIDs(logger, presenceIDs, envelope, reliable)
}

Benefits:

  • Enhanced control over real-time messaging within Nakama.
  • Improved support for in-game communication.
  • Potential for more efficient and targeted notifications.

Proposal:
I propose adding this feature to Nakama as a built-in functionality. This would require extending Nakama’s core features to support sending notifications to specific connections while maintaining backward compatibility.

Next Steps:
I’m open to discussing the technical details of my implementation and working together with the Nakama community to refine this feature and integrate it into Nakama’s codebase.

If my suggestion is good, let me know and I will create a pull request

I’d love to hear your thoughts, feedback, and suggestions on this proposal. Please share your opinions and ideas, and let’s work together to make Nakama even more powerful for real-time multiplayer game development.

Thank you for your attention and support.

Hello @ThanhPhucHuynh,

Nakama supports multiple socket connections per user, however it is not designed for multiple games on a single instance - all the existing APIs are intended for a single game running on Nakama.

For this reason we’re not considering adding support for this use case.

Best

1 Like

Thank you so much @sesposito