Last active
September 28, 2016 09:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PFUser *user = [PFUser currentUser]; | |
PNChannel *channel = [PNChannel channelWithName:user.objectId]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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