Skip to content

Instantly share code, notes, and snippets.

@pandurang90
Last active March 21, 2017 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandurang90/8a58452ff9ed3f3b3775 to your computer and use it in GitHub Desktop.
Save pandurang90/8a58452ff9ed3f3b3775 to your computer and use it in GitHub Desktop.
opentok.js
<%= form_tag '#', class: 'chat_form' do %>
<%= text_field_tag ‘message’, id: ‘chat_msg’%>
<%= submit_tag ‘send’%>
<% end %>
<script src='//static.opentok.com/v2/js/opentok.min.js'></script>
<script type="text/javascript">
$(document).ready(function(){
var apiKey = <%= OPENTOK_API_KEY %>
// use same session_id and token that we have generated above.
var sessionID = "<%= @session_id %>"
var token = "<%= @token %>"
var session = OT.initSession(apiKey, sessionID);
session.connect(token, function(error) {
if (error) {
console.log('Unable to connect: ', error.message);
}
else {console.log('Connected to the session.');}
});
// send message to text_chat channel using signal method on form submit
$(".chat_form").submit(function(event) {
event.preventDefault();
if($('#chat_msg').val() != ""){
session.signal({
data:$('#chat_msg').val(),
type:"text_chat"
},
function(error) {
$('#chat_msg').val('');
}
);
}
});
// listen for the signal on text_chat channel
// in order to receive message sent to channel text_chat
session.on('signal:text_chat', function(event) {
message = event.data
// do some stuff here with message,
// maybe show in chat window/store in database if needed
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment