Parse + PubNub Publish Messages
You can publish messages inside of parse cloud to your mobile users to create chat experiences easily. If you want to send the messages directly from within Parse Cloud use the following code to send a message to a user:
Publish PubNub Message on Parse Cloud
Parse.Cloud.httpRequest({
url: 'https://pubsub.pubnub.com/publish/<PUB-KEY>/<SUB-KEY>/0/<USER-CHANNEL-NAME>/0/%22Hellooooooo!%22',
// successful HTTP status code
success: function(httpResponse) {
console.log(httpResponse.text);
},
// unsuccessful HTTP status code
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});
You don't necessarily need to use Parse this way. You can instead send messages directly between users to each user's channel name. Sally has "channel-sally"
, and Bill has "channel-bill"
. You can publish and subscribe directly form your Objective-C App Code. Bill will subscribe to his own channel and a Sally to her channel.
Bob's App
// Define a channel
PNChannel *myChannel = [PNChannel channelWithName:@"channel-bob"];
PNChannel *friendChannel = [PNChannel channelWithName:@"channel-sally"];
// Receive Messages Sent to Me!
[PubNub subscribeOnChannel:myChannel];
// Send a Message to Sally
[PubNub sendMessage:@"Hello from PubNub iOS!" toChannel:friendChannel];
//(In AppDelegate.m, define didReceiveMessage delegate method:)
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
NSLog(@"Received: %@", message.message);
}
Bob can receive messages on his own channel and he can send messages to Sally or any other of his friends.