Conversation.js is inspired by skype; and it provides simple events-like API to manage conversations, enable/disable media devices; add/download files; and do anything supported by Skype.
It allows you open data conversation between two or more users using their user-ids.
It is MIT Licenced, which means that you can use it in any commercial/non-commercial product, free of cost.
npm install conversationjs
To use it:
<script src="./node_modules/conversationjs/conversation.js"></script>
-
AndroidRTC
<li>
<a href="https://www.webrtc-experiment.com/Conversationjs/search-user.html">Search Users</a>
</li>
<li>
<a href="https://www.webrtc-experiment.com/Conversationjs/cross-language-chat.html">Cross-Language (Multi-Lingual) Text Chat</a>
</li>
<li>
<a href="https://www.rtcmulticonnection.org/conversationjs/demos/">Old Conversation.js demos</a>
</li>
Gif Presentation
Link the library
<script src="//cdn.webrtc-experiment.com/RTCMultiConnection.js"></script>
<script src="//cdn.webrtc-experiment.com/conversation.js"></script>
=
Open simple conversation between two users
var websocket = new WebSocket('ws://domain:port/');
var signaler = new Signaler();
signaler.on('message', function(message) {
websocket.send(JSON.stringify(message));
});
websocket.onmessage = function(event) {
var message = JSON.parse(event.data);
signaler.emit('message', message);
});
var user = new User();
signaler.connect(user);
user.openconversationwith('target-username');
user.on('conversation-opened', function (conversation) {
conversation.on('message', function (event) {
console.log(event.username, event.data);
});
conversation.on('media-enabled', function (media) {
media.emit('join-with', 'microphone');
});
});
How to use socket.io?
var socket = io.connect();
var signaler = new Signaler();
signaler.on('message', function(message) {
socket.emit('message', message);
});
socket.on('message', function(message) {
signaler.emit('message', message);
});
signaler.connect(user);
How to use WebSockets?
var websocket = new WebSocket('ws://domain:port/');
var signaler = new Signaler();
signaler.on('message', function(message) {
websocket.send(JSON.stringify(message));
});
websocket.onmessage = function(event) {
var message = JSON.parse(event.data);
signaler.emit('message', message);
});
signaler.connect(user);
How to set defaults?
"defaults" are default properties, objects and methods that are applied to RTCMultiConnection object.
See list of all such properties here: http://www.rtcmulticonnection.org/docs/
user.defaults = {
log: true,
trickleIce: true,
getExternalIceServers: false,
leaveOnPageUnload: true,
iceServers: [{
url: 'stun:stun.l.google.com:19302'
}],
iceProtocols: {
tcp: true,
udp: true
},
candidates: {
host: true,
reflexive: true,
relay: true
},
autoReDialOnFailure: false,
body: document.body || document.documentElement
};
How to accept/reject friend requests?
user.on('friend-request', function (request) {
if (window.confirm('Do you want to accept friend-request made by ' + request.sender + '?')) {
request.accept();
} else {
request.reject();
}
});
How to check friend-request status?
user.on('request-status', function (request) {
if (request.status == 'accepted') {
alert(request.sender + ' accepted your request.');
}
if (request.status == 'rejected') {
alert(request.sender + ' rejected your request.');
}
});
How to emit events to multiple users?
document.querySelector('#chat-message').onchange = function (event) {
user.peers.emit('message', this.value);
};
document.querySelector('#enable-microphone').onclick = function () {
user.peers.emit('enable', 'microphone');
};
document.querySelector('#enable-camera').onclick = function () {
user.peers.emit('enable', 'camera');
};
document.querySelector('#enable-screen').onclick = function () {
user.peers.emit('enable', 'screen');
};
How to share files?
document.querySelector('input[type=file]').onchange = function () {
user.peers.emit('add-file', this.files);
};
How to check if target user added file?
conversation.on('add-file', function (file) {
file.download();
});
How to check file-download progress?
conversation.on('file-progress', function (progress) {
console.log('percentage %', progress.percentage);
});
How to save downloaded file to disk?
conversation.on('file-downloaded', function (file) {
file.savetodisk();
});
How to check if file is successfully sent?
conversation.on('file-sent', function (file) {
console.log(file.name, 'sent.');
});
How to check if target user refused to receive your file?
conversation.on('file-cancelled', function (file) {
console.log(file.name, 'cancelled.');
});
Demos
Credits
Muaz Khan:
- Personal Webpage: http://www.muazkhan.com
- Email: muazkh@gmail.com
- Twitter: https://twitter.com/muazkh and https://twitter.com/WebRTCWeb
- Google+: https://plus.google.com/+WebRTC-Experiment
- Facebook: https://www.facebook.com/WebRTC
License
Conversation.js is released under MIT licence . Copyright (c) Muaz Khan.