Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import Video from './video'; // pull in our Video collection so we can add/edit/delete from this file's code
  2. import { Meteor } from 'meteor/meteor';
  3. import OpenTok from 'opentok'; // pull in the opentok npm package
  4.  
  5.  
  6. Meteor.methods({
  7. 'video.createSession': function(documentId){
  8. check(documentId, String); // we will pass the video record _id in from the client, which as this point is hard coded as '1'
  9.  
  10. // first we'll grab our api keys and create a new opentok instance
  11. const opentok = new OpenTok(Meteor.settings.public.opentok.apiKey, Meteor.settings.public.opentok.apiSecret);
  12.  
  13. //next we'll make the opentok async npm package's call into a syncrhonous call with Meteor's wrapAsync function
  14. let createSessionSync = Meteor.wrapAsync(opentok.createSession, opentok);
  15.  
  16. let sessionId; // create a variable where we can store the sessionId we get back from opentok's api
  17.  
  18. // then we try/catch the call to opentok
  19. try {
  20. const response = createSessionSync(); // this will make the api call and store response in the 'response' variable
  21. console.log(response.sessionId); // this wont run until we get a response back
  22. sessionId = response.sessionId; // store the sessionId from the response into the sessionId variable
  23.  
  24. } catch (err) {
  25. console.log(err);
  26. throw new Meteor.Error('oops', err); // throw an error if we have problems hitting the tokbox api
  27. }
  28. // update the video record with the sessionId we got from tokbox, and turn sessionOn to true
  29. Video.update({_id: documentId}, {$set: { currentSessionId: sessionId, sessionOn: true}});
  30. // generate a token to send back to the client, we need the sessionId to do this (but it happens on npm/locally, we dont call tokbox api... I dont think we do atleast)
  31. const token = opentok.generateToken(sessionId); // generate token
  32. return token; // return token
  33.  
  34. },
  35. 'video.endSession': function(documentId){
  36. check(documentId, String);
  37.  
  38. Video.update({_id: documentId}, {$set: { sessionOn: false}});
  39. return;
  40.  
  41. },
  42. 'video.joinSession': function(documentId, user){
  43. check(documentId, String);
  44.  
  45. const opentok = new OpenTok(Meteor.settings.opentok.apiKey, Meteor.settings.opentok.apiSecret); // create openTok instance
  46.  
  47. let video = Video.findOne({_id: documentId}); // find the relevant video record
  48. const token = opentok.generateToken(video.currentSessionId); // generate a token for the user
  49. return token; // return the token
  50.  
  51.  
  52. }
  53. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement