Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active September 28, 2016 09:20
Show Gist options
  • Save stephenlb/ec7f38d4f106c3333ec4 to your computer and use it in GitHub Desktop.
Save stephenlb/ec7f38d4f106c3333ec4 to your computer and use it in GitHub Desktop.
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