Use Rich Message
Message is more than text. Rich content (such as image, video, audio, etc.) is very important for user experience. Magnet Message has built-in support for rich message by using attachments. The following illustrates how you can send rich message using our APIs.
Send Message with Attachment
The following use case describes how to send a message with an attachment.
// get window.File object from <input type="file" />. multiple files can be uploaded at the same time using the "multiple" form attribute
var myFiles = el.files;
// add any message content you would like to be sent along with the attachment
var msg = new Max.Message({
type: 'picture'
});
myChannel.publish(msg, myFiles).success(function() {
// sent the attachment!
}).error(function(err) {
// handle error
});
Receive Message and Attachment
The following receives the message and attachment.
var listener = new Max.EventListener('myListener', {
message: function(receivedMessage) {
// message has been received
var from = receivedMessage.sender.userName;
// do something with received attachment's download url
console.log(receivedMessage.attachments[0].downloadUrl);
}
});
Access Rich Content
The attachment object provides a URL to access the rich content. This URL can be injected directly into an \
tag if it is an image, or provided as a download link by injecting within a hyperlink (\).
// add url to image using plain JS <img id="my-profile-pic" src="" />
document.getElementById('my-profile-pic').src = receivedMessage.attachments[0].downloadUrl;
// add url to link using jQuery <a id="my-download-link" href="">Start Download</a>
$('#my-download-link').attr('href', receivedMessage.attachments[0].downloadUrl);
Next Steps
See how to create a Public Forum in your app.