This article describes the best practice of the long polling mechanism in MNS.

Background information

  • MNS provides a long polling mechanism for the ReceiveMessage operation. Valid values of the WaitSecond parameter: 1 to 30. An MNS server holds long polling requests until messages are available on the server. The long polling mechanism allows you to receive messages in real time without the need to send a large number of invalid requests. We recommend that you use the long polling mechanism that is provided by MNS.

    For more information about how to specify the parameters for long polling, see ReceiveMessage.

  • HTTP long polling connections remain effective on MNS servers within a specified period and consume few resources. To avoid malicious attacks, MNS limits the number of connections to each queue for a single user. For more information, see Limits.


You can use hundreds of threads on a single machine to access an MNS server to retrieve messages. If you use multiple machines, thousands of long polling requests are sent at a time.

If no messages exist in the queue and you send another long polling request, the MNS server returns the following error message: Message not found.

If you use a while loop to send long polling requests without resolving the error, a large number of invalid requests are sent to the server.


If you use hundreds of threads to access an MNS server at a time and no message exists in the queue, you do not need to hold all the threads on the server. You can specify whether to hold one or more threads for long polling based on your needs. If the specified threads detect a message in the queue, they can wake up other threads to receive the message.

MNS provides the MessageReceiver class to retrieve messages. For more information about the sample code, see the LimitedLongPolling package. An object named MessageReceiver is created for each thread and the object calls the receiver.receiveMessage operation to retrieve messages.

MessageReceiver provides an exclusive long polling mechanism. If one thread is used to hold long polling requests, other threads are in the waiting status.