Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing messages from Parse via PubNub

I would like to use PubNub with Parse for a chat module. Could somebody explain me how can i send messages with text and images via PubNub to a user (only one-to-one)? I wanna use the PFUser usernames as the id of a user's private channel.

I've found this code in the PubNub help docs, but i've never seen code like this in objective-c. Where/how should i use this line?

curl https://pubsub.pubnub.com/publish/<PUB-KEY>/<SUB-KEY>/0/<CHANNEL-NAME>/0/%22Hellooooooo%22

It's also unclear for me that where should i store the messages? In Parse or can i store them only at PubNub? I'm not sure that the second variation is possible, because i didn't see any data storage at PubNub. Or for example i'm sending only the url's of the PFObjects that i store at Parse?

like image 390
rihe Avatar asked May 07 '14 22:05

rihe


People also ask

Are publish and subscribe in PubNub in send and receive messages?

The publish() function is used to send a message to all subscribers of a channel. To publish a message you must first specify a valid publishKey at initialization. A successfully published message is replicated across the PubNub Real-Time Network and sent simultaneously to all subscribed clients on a channel.

What are published and described in PubNub in order to send and receive messages?

Sending Messages​ The primary means for sending messages is using the Publish API. You may only send a message to one channel at a time. PubNub's network supports sending unlimited publishes without waiting for response on the same TCP socket connection.

How do you send a message on PubNub?

Sending a message​ You can publish a message as a string, or send any data (JSON data, or an object), as long as the data size is below 32 KiB. pubNub. publish() . message("Hello World!") .channel("ch-1") .

What is the use of PubNub?

PubNub is an add-on that lets you connect, scale, and manage realtime applications and IoT devices without having to manage realtime infrastructure. Use PubNub to send/receive messages in real time across multiple platforms and devices with two simple API calls (Publish/Subscribe - Send/Receive).


1 Answers

Parse + PubNub Publish Messages

PubNub Parse

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.

like image 124
Stephen Blum Avatar answered Oct 15 '22 12:10

Stephen Blum