Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combining Parse API with PubNub Realtime Messaging - By combining the Parse API with PubNub Data Streams you will have a better starting point rather than building a custom backend from scratch.
var pubnub = {
'publish_key' : 'demo',
'subscribe_key' : 'demo'
};
var bob_channel = "channel-bob";
var sally_channel = "channel-sally";
var message = {
"from" : "Sally",
"to" : "Bob",
"message" : "Hi there Bob!"
};
Parse.Cloud.httpRequest({
url: 'http://pubsub.pubnub.com/publish/' +
pubnub.publish_key + '/' +
pubnub.subscribe_key + '/0/' +
bob_channel + '/0/' +
encodeURIComponent(JSON.stringify(message)),
// SUCCESS CALLBACK
success: function(httpResponse) {
console.log(httpResponse.text);
// httpResponse.text -> [1,"Sent","14090206970886734"]
},
// You should consider retrying here when things misfire
error: function(httpResponse) {
console.error('Request failed ' + httpResponse.status);
}
});
PFUser *user = [PFUser currentUser];
PNChannel *channel = [PNChannel channelWithName:user.objectId];
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment