4

I am building a user location tracking application for a client.

I am getting the location via Client (Android) using SignalR Hub Method and Saving in MongoDb

My Code

public async Task UpdateLocation(double latitude, double longitude)
{
    await _mediator.Send(new UpdateLocationRequestModel()
    {
        Latitude = latitude,
        Longitude = longitude,
        UserId = Context.User.GetUserId()
    });
}

Now I want another Client (Web) to view that updates of a specific user in real time.

How can I do that?

My Theoretical Solution

I was thinking of creating a method in the Hub as when the Client (Web) pushes a button a method will be called

public async Task ShowUser(string userId)
{
    //Add To Redis Cache ('track',userId)         
}

public async Task UpdateLocation(double latitude, double longitude)
{
    await _mediator.Send(new UpdateLocationRequestModel()
    {
        Latitude = latitude,
        Longitude = longitude,
        UserId = Context.User.GetUserId()
    });
    // Check Redis Cahce for 'track'
    // Send Data to Client(Web)
}

I haven't tested this but is there a better way?

Kiril1512
  • 3,231
  • 3
  • 16
  • 41
test noob
  • 43
  • 1
  • 5

1 Answers1

1

If you are only using SignalR

A theoretical Approach for you without using Redis would be this. Assuming you only want to track one user at a time

public async AddToTrackGroup(string userId) {
        return Groups.Add(Context.ConnectionId, "track-" + userId);
}

public async RemoveFromTrackGroup(string userId) {
        return Groups.Remove(Context.ConnectionId, "track-" + userId);
}

public async Task UpdateLocation(double latitude, double longitude)
{
    await _mediator.Send(new UpdateLocationRequestModel()
    {
        Latitude = latitude,
        Longitude = longitude,
        UserId = Context.User.GetUserId()
    });
    Clients.Group("track-" + Context.User.GetUserId()).updateLocation(latitude,longitude);
}

Theoretically this would give you access to the clients location as a Group exists for that track user and also multiple clients can also track that user.

I haven't tested it but this could work.

If any issue in the code or approach do not hesitate to correct me.

As I do believe this could help other users.

Noob Coder
  • 444
  • 1
  • 5
  • 16