ProximityDevice.SubscribeForMessage | subscribeForMessage method

Applies to Windows and Windows Phone

Creates a subscription for a specified message type.

Syntax


var number = proximityDevice.subscribeForMessage(messageType, messageReceivedHandler);

Parameters

messageType

Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]

The type of message to deliver to this subscription.

messageReceivedHandler

Type: MessageReceivedHandler

The handler that the proximity provider will call when it delivers a message.

Return value

Type: Number [JavaScript] | System.Int64 [.NET] | int64 [C++]

A unique ID for the subscription.

Remarks

After your app calls the SubscribeForMessage method, it will receive messages that are published with the same message type (as indicated by the messageType parameter) from any proximate devices. For details on the different message types, see the remarks in the PublishBinaryMessage(String, IBuffer) reference topic.

You can cancel a subscription by passing the subscription ID that's returned by the SubscribeForMessage method to the StopSubscribingForMessage method.

You can publish a message to a subscriber by using the PublishMessage, PublishBinaryMessage, or PublishUriMessage method.

Important  

The proximity APIs do not provide authentication, encryption, or message integrity. Do not use proximity to exchange user sensitive information such as passwords, financial data, text messages, email, photographs, or government id numbers.

Examples


function id(elementId) {
    return document.getElementById(elementId);
}

var proximityDevice;
var publishedMessageId = -1;
var subscribedMessageId = -1;

function initializeProximityDevice() {
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.getDefault();

    if (proximityDevice) {
        id("publishMessageButton").addEventListener("click", publishMessage);
        id("subscribeForMessageButton").addEventListener("click", subscribeForMessage);
id("stopSubscribingForMessageButton").addEventListener("click", 
    stopSubScribingForMessage);
id("stopPublishingMessageButton").addEventListener("click", 
    stopPublishingMessage);
    }
    else {
        id("messageDiv").innerHTML += "Failed to initialized proximity device.<br />";
    }
}

function publishMessage() {
    // Stop publishing the current message.
    if (publishedMessageId != -1) {
        proximityDevice.stopPublishingMessage(publishedMessageId);
    }

    publishedMessageId = 
proximityDevice.publishMessage("Windows.SampleMessage", id("messageText").value);
}

function subscribeForMessage() {
    // Only subscribe for the message one time.
    if (subscribedMessageId === -1) {
        subscribedMessageId = 
    proximityDevice.subscribeForMessage("Windows.SampleMessage", messageReceived);
    }
}

function messageReceived(device, message) {
    id("messageDiv").innerHTML += "Message received: " + message.dataAsString + "<br />";
}

    function stopSubscribingForMessage() {
proximityDevice.stopSubscribingForMessage(subscribedMessageId);
    subscribedMessageId = -1;
    }

    function stopPublishingMessage() {
proximityDevice.stopPublishingMessage(publishedMessageId);
    publishedMessageId = -1;
    }


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

Windows.Networking.Proximity
Windows::Networking::Proximity [C++]

Metadata

Windows.winmd

Capabilities

proximity
ID_CAP_PROXIMITY [Windows Phone]

See also

ProximityDevice
Proximity and Tapping (JavaScript)
Proximity and Tapping (C#/VB/C++)
Samples
Proximity sample

 

 

Show:
© 2014 Microsoft