Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // Join the admin channel
  2. app.channel('admins').join(connection);
  3.  
  4. // Leave the admin channel
  5. app.channel('admins').leave(connection);
  6.  
  7. // Leave a channel conditionally
  8. app.channel('admins', 'rooms').leave(connection => connection.userId === user._id);
  9.  
  10. // Leave all room channels
  11. const roomChannels = app.channels.filter(channel => channel.indexOf('room/') === 0);
  12.  
  13. app.channel(roomChannels).leave(connection);
  14.  
  15. // Leave all channels
  16. app.channel(app.channels).leave(connection);
  17.  
  18. // Example: Join an anonymous channel
  19. app.on('connection', connection => {
  20. // Add to the anonymous channel
  21. app.channel('anonymous').join(connection);
  22. });
  23.  
  24. // Example: Join the users rooms
  25. app.on('login', (payload, meta) => {
  26. const connection = meta.connection;
  27.  
  28. // Connection can be undefined e.g. when logging in via REST
  29. if(connection) {
  30. // Leave anonymous channel first
  31. app.channel('anonymous').leave(connection);
  32.  
  33. // Get the user object and stick into channels
  34. app.service('users').get(payload.userId).then(user => {
  35. // A channel just for this user
  36. app.channel(`users/${user._id}`).join(connection);
  37.  
  38. // Put user into the chat rooms they joined
  39. user.rooms.forEach(roomId => {
  40. app.channel(`rooms/${roomId}`).join(connection);
  41. });
  42. });
  43. }
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement