User not found returned by Azure web pubsub rest api

Nayan Das 71 Reputation points
2021-07-20T06:50:50.243+00:00

We have created the webhooks for /connect and /connected in our server and we get those endpoints called when client connects to pubsub using websocket. We want to add the connected user to be added to certain pubsub groups and we do that in /connected webhook. The problem is sometimes the pubsub rest api throws User not found error when trying to add the user to a group.

If addition of user to the group fails, the server disconnects the current connection and the client reconnects. This is resulting in an endless loop of client reconnection since the pubsub rest api constantly throws "User not found" in /connected webhook.

Solutions we have tried:

  • Add user to group in /connect endpoint instead of /connected. Same error gets thrown.
  • Add connection to group instead of user. We are currently monitoring that and would get back with our observation.

Note: The server is not using pubsub rest api directly. Its using azure messaging python sdk. Rest api here

Azure Web PubSub
Azure Web PubSub
An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
59 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Liangying Wei 756 Reputation points Microsoft Employee
    2021-07-21T01:53:30.587+00:00

    The problem is sometimes the pubsub rest API throws User not found error when trying to add the user to a group.

    This sounds like a service bug that the user is not yet completely registered when the connected event is triggered. Are you using Free tier or Standard tier? Does it happen frequently? It would be appreciated if you could provide a min-repro project/steps.

    There is one convenient way to add the connection to a group when /connect, to leverage the CloudEvents response groups in the connect response body: https://github.com/Azure/azure-webpubsub/blob/main/docs/references/protocol-cloudevents.md#success-response-format

    The property provides a convenient way for users to add this connection to one or multiple groups. In this way, there is no need to have an additional call to add this connection to some group.

    1 person found this answer helpful.

  2. Nayan Das 71 Reputation points
    2021-07-22T10:32:24.33+00:00

    We have upgraded the plan of pubsub to Standard tier and we are currently monitoring the application. When our pubsub was in free tier, the problem occurred atleast once daily.

    Below the post function handles the POST /connected webhook endpoint

    class ConnectedEventHandlerView(APIView):
        def post(self, request):
            user_id = request.headers["Ce-Userid"]
            connection_id = request.headers["Ce-Connectionid"]
    
            try:
                users = User.objects.filter(pk=user_id)
                if users:
                    user = users[0]
    
                pubsub_group_name = create_own_pubsub_group(user)
                response = pubsub_service_client.send_request(
                    build_add_user_to_group_request(
                        settings.AZURE_PUBSUB_HUB_NAME,
                        pubsub_group_name,
                        user_id,
                    )
                )
    
                if response.status_code == 200:
                    return Response()
                else:
                    local_logger.error(
                        f"status code {response.status_code}, response body {response.text}"
                    )
                    logger.error(
                        f"Could not add connection to groups. user_id {user_id}, connection_id f{connection_id}"
                    )
    
                    raise Exception("Could not add connection to groups")
            except:
                response = pubsub_service_client.send_request(
                    build_close_client_connection_request(
                        settings.AZURE_PUBSUB_HUB_NAME, connection_id
                    )
                )
    
                return Response(status=400)