Best JavaScript code snippet using ava
Scalable-Broadcast.js
Source:Scalable-Broadcast.js
1// Muaz Khan - www.MuazKhan.com2// MIT License - www.WebRTC-Experiment.com/licence3// Documentation - github.com/muaz-khan/RTCMultiConnection45// pushLogs is used to write error logs into logs.json6var pushLogs = require('./pushLogs.js');78var users = {};910module.exports = exports = function(config, socket, maxRelayLimitPerUser) {11 try {12 maxRelayLimitPerUser = parseInt(maxRelayLimitPerUser) || 2;13 } catch (e) {14 maxRelayLimitPerUser = 2;15 }1617 socket.on('join-broadcast', function(user) {18 try {19 if (!users[user.userid]) {20 socket.userid = user.userid;21 socket.isScalableBroadcastSocket = true;2223 users[user.userid] = {24 userid: user.userid,25 broadcastId: user.broadcastId,26 isBroadcastInitiator: false,27 maxRelayLimitPerUser: maxRelayLimitPerUser,28 relayReceivers: [],29 receivingFrom: null,30 canRelay: false,31 typeOfStreams: user.typeOfStreams || {32 audio: true,33 video: true34 },35 socket: socket36 };3738 notifyBroadcasterAboutNumberOfViewers(user.broadcastId);39 }4041 var relayUser = getFirstAvailableBroadcaster(user.broadcastId, maxRelayLimitPerUser);4243 if (relayUser === 'ask-him-rejoin') {44 socket.emit('rejoin-broadcast', user.broadcastId);45 return;46 }4748 if (relayUser && user.userid !== user.broadcastId) {49 var hintsToJoinBroadcast = {50 typeOfStreams: relayUser.typeOfStreams,51 userid: relayUser.userid,52 broadcastId: relayUser.broadcastId53 };5455 users[user.userid].receivingFrom = relayUser.userid;56 users[relayUser.userid].relayReceivers.push(57 users[user.userid]58 );59 users[user.broadcastId].lastRelayuserid = relayUser.userid;6061 socket.emit('join-broadcaster', hintsToJoinBroadcast);6263 // logs for current socket64 socket.emit('logs', 'You <' + user.userid + '> are getting data/stream from <' + relayUser.userid + '>');6566 // logs for target relaying user67 relayUser.socket.emit('logs', 'You <' + relayUser.userid + '>' + ' are now relaying/forwarding data/stream to <' + user.userid + '>');68 } else {69 users[user.userid].isBroadcastInitiator = true;70 socket.emit('start-broadcasting', users[user.userid].typeOfStreams);7172 // logs to tell he is now broadcast initiator73 socket.emit('logs', 'You <' + user.userid + '> are now serving the broadcast.');74 }75 } catch (e) {76 pushLogs(config, 'join-broadcast', e);77 }78 });7980 socket.on('scalable-broadcast-message', function(message) {81 socket.broadcast.emit('scalable-broadcast-message', message);82 });8384 socket.on('can-relay-broadcast', function() {85 if (users[socket.userid]) {86 users[socket.userid].canRelay = true;87 }88 });8990 socket.on('can-not-relay-broadcast', function() {91 if (users[socket.userid]) {92 users[socket.userid].canRelay = false;93 }94 });9596 socket.on('check-broadcast-presence', function(userid, callback) {97 // we can pass number of viewers as well98 try {99 callback(!!users[userid] && users[userid].isBroadcastInitiator === true);100 } catch (e) {101 pushLogs(config, 'check-broadcast-presence', e);102 }103 });104105 socket.on('get-number-of-users-in-specific-broadcast', function(broadcastId, callback) {106 try {107 if (!broadcastId || !callback) return;108109 if (!users[broadcastId]) {110 callback(0);111 return;112 }113114 callback(getNumberOfBroadcastViewers(broadcastId));115 } catch (e) {}116 });117118 function getNumberOfBroadcastViewers(broadcastId) {119 try {120 var numberOfUsers = 0;121 Object.keys(users).forEach(function(uid) {122 var user = users[uid];123 if (user.broadcastId === broadcastId) {124 numberOfUsers++;125 }126 });127 return numberOfUsers - 1;128 } catch (e) {129 return 0;130 }131 }132133 function notifyBroadcasterAboutNumberOfViewers(broadcastId, userLeft) {134 try {135 if (!broadcastId || !users[broadcastId] || !users[broadcastId].socket) return;136 var numberOfBroadcastViewers = getNumberOfBroadcastViewers(broadcastId);137138 if (userLeft === true) {139 numberOfBroadcastViewers--;140 }141142 users[broadcastId].socket.emit('number-of-broadcast-viewers-updated', {143 numberOfBroadcastViewers: numberOfBroadcastViewers,144 broadcastId: broadcastId145 });146 } catch (e) {}147 }148149 // this even is called from "signaling-server.js"150 socket.ondisconnect = function() {151 try {152 if (!socket.isScalableBroadcastSocket) return;153154 var user = users[socket.userid];155156 if (!user) return;157158 if (user.isBroadcastInitiator === false) {159 notifyBroadcasterAboutNumberOfViewers(user.broadcastId, true);160 }161162 if (user.isBroadcastInitiator === true) {163 // need to stop entire broadcast?164 for (var n in users) {165 var _user = users[n];166167 if (_user.broadcastId === user.broadcastId) {168 _user.socket.emit('broadcast-stopped', user.broadcastId);169 }170 }171172 delete users[socket.userid];173 return;174 }175176 if (user.receivingFrom || user.isBroadcastInitiator === true) {177 var parentUser = users[user.receivingFrom];178179 if (parentUser) {180 var newArray = [];181 parentUser.relayReceivers.forEach(function(n) {182 if (n.userid !== user.userid) {183 newArray.push(n);184 }185 });186 users[user.receivingFrom].relayReceivers = newArray;187 }188 }189190 if (user.relayReceivers.length && user.isBroadcastInitiator === false) {191 askNestedUsersToRejoin(user.relayReceivers);192 }193194 delete users[socket.userid];195 } catch (e) {196 pushLogs(config, 'scalable-broadcast-disconnect', e);197 }198 };199200 return {201 getUsers: function() {202 try {203 var list = [];204 Object.keys(users).forEach(function(uid) {205 var user = users[uid];206 if(!user) return;207 208 try {209 var relayReceivers = [];210 user.relayReceivers.forEach(function(s) {211 relayReceivers.push(s.userid);212 });213214 list.push({215 userid: user.userid,216 broadcastId: user.broadcastId,217 isBroadcastInitiator: user.isBroadcastInitiator,218 maxRelayLimitPerUser: user.maxRelayLimitPerUser,219 relayReceivers: relayReceivers,220 receivingFrom: user.receivingFrom,221 canRelay: user.canRelay,222 typeOfStreams: user.typeOfStreams223 });224 }225 catch(e) {226 pushLogs('getUsers', e);227 }228 });229 return list;230 }231 catch(e) {232 pushLogs('getUsers', e);233 }234 }235 };236};237238function askNestedUsersToRejoin(relayReceivers) {239 try {240 var usersToAskRejoin = [];241242 relayReceivers.forEach(function(receiver) {243 if (!!users[receiver.userid]) {244 users[receiver.userid].canRelay = false;245 users[receiver.userid].receivingFrom = null;246 receiver.socket.emit('rejoin-broadcast', receiver.broadcastId);247 }248249 });250 } catch (e) {251 pushLogs(config, 'askNestedUsersToRejoin', e);252 }253}254255function getFirstAvailableBroadcaster(broadcastId, maxRelayLimitPerUser) {256 try {257 var broadcastInitiator = users[broadcastId];258259 // if initiator is capable to receive users260 if (broadcastInitiator && broadcastInitiator.relayReceivers.length < maxRelayLimitPerUser) {261 return broadcastInitiator;262 }263264 // otherwise if initiator knows who is current relaying user265 if (broadcastInitiator && broadcastInitiator.lastRelayuserid) {266 var lastRelayUser = users[broadcastInitiator.lastRelayuserid];267 if (lastRelayUser && lastRelayUser.relayReceivers.length < maxRelayLimitPerUser) {268 return lastRelayUser;269 }270 }271272 // otherwise, search for a user who not relayed anything yet273 // todo: why we're using "for-loop" here? it is not safe.274 var userFound;275 for (var n in users) {276 var user = users[n];277278 if (userFound) {279 continue;280 } else if (user.broadcastId === broadcastId) {281 // if (!user.relayReceivers.length && user.canRelay === true) {282 if (user.relayReceivers.length < maxRelayLimitPerUser && user.canRelay === true) {283 userFound = user;284 }285 }286 }287288 if (userFound) {289 return userFound;290 }291292 // need to increase "maxRelayLimitPerUser" in this situation293 // so that each relaying user can distribute the bandwidth294 return broadcastInitiator;295 } catch (e) {296 pushLogs(config, 'getFirstAvailableBroadcaster', e);297 }
...
database.js
Source:database.js
...9 if(error) {10 console.error(error);11 }12 if(client) { 13 wss.broadcast("Database Connected");14 client.exec("select TOP 25 * from \"PO.Header\"",15 function (err, res, cb) {16 if (err){ return ("ERROR: " + err); }17 wss.broadcast("Database Call Complete");18 for(var i = 0; i < res.length; i++){19 wss.broadcast(res[i].PURCHASEORDERID + ": " + res[i].GROSSAMOUNT + "\n");20 }21 client.disconnect(function(cb){wss.broadcast("Database Disconnected"); pool.release(client); } );22 });23 } //End if client24 }); //end create connection 25 cb(); 26 }, //end callHANA27 callHANA1: function(cb, wss){28 //hdb.createConnection(hanaService, function(error, client) {29 pool.acquire(function(error, client){30 if(error) {31 console.error(error);32 }33 if(client) {34 async.waterfall([35 function execute(callback){36 wss.broadcast("Database Connected #1");37 client.exec("select TOP 25 * from \"PO.Header\"", 38 function(err,res){39 if (err){ return ("ERROR: " + err); }40 callback(null,err,res);});41 },42 function processResults(err, res, callback){43 if (err){ return ("ERROR: " + err); }44 wss.broadcast("Database Call #1");45 wss.broadcast("--PO Header"); 46 for(var i = 0; i < res.length; i++){47 wss.broadcast(res[i].PURCHASEORDERID + ": " + res[i].GROSSAMOUNT);48 }49 wss.broadcast("\n"); 50 client.disconnect(callback); 51 },52 function disconnectDone(callback){53 wss.broadcast("Database Disconnected #1"); 54 wss.broadcast("End Waterfall #1");55 pool.release(client);56 cb();57 }58 ], function (err, result) {59 wss.broadcast(err || "done");60 wss.broadcast("Error Occured disrupting flow of Waterfall for #1");61 pool.release(client);62 cb();63 }); //end Waterfall64 } //end if client65 }); //end create connection66 }, //end callHANA167 callHANA2: function(cb, wss){68 //hdb.createConnection(hanaService, function(error, client) {69 pool.acquire(function(error, client){70 if(error) {71 console.error(error);72 }73 if(client) {74 async.waterfall([75 function execute(callback){76 wss.broadcast("Database Connected #2");77 client.exec("select TOP 25 * from \"PO.Item\"", 78 function(err,res){79 if (err){ return ("ERROR: " + err); }80 callback(null,err,res);});81 },82 function processResults(err, res, callback){83 if (err){ return ("ERROR: " + err); }84 wss.broadcast("Database Call #2");85 wss.broadcast("--PO Items"); 86 for(var i = 0; i < res.length; i++){87 wss.broadcast(res[i].PURCHASEORDERID + ": " + res[i].PURCHASEORDERITEM );88 }89 wss.broadcast("\n"); 90 client.disconnect(callback); 91 },92 function disconnectDone(callback){93 wss.broadcast("Database Disconnected #2"); 94 wss.broadcast("End Waterfall #2");95 pool.release(client);96 cb();97 }98 ], function (err, result) {99 wss.broadcast(err || "done");100 wss.broadcast("Error Occured disrupting flow of Waterfall for #2");101 pool.release(client);102 cb();103 }); //end Waterfall104 } //end if client105 }); //end create connection106 } //end callHANA2...
SubscriberTest.js
Source:SubscriberTest.js
...16 assert.equal(data, 'test broadcast');17 subscription.unsubscribeAll();18 done();19 });20 subscription.broadcast('test broadcast');21 });22 it('Test multi-broadcast', function(done){23 var count = 0;24 function partial(){25 ++count;26 if(count === 2){27 subscription.unsubscribeAll();28 done();29 }30 }31 subscription.subscribe('dirwin', (data) => {32 assert.equal(data, 'test broadcast');33 partial();34 });35 subscription.subscribe('other', (data) => {36 assert.equal(data, 'test broadcast');37 partial();38 });39 subscription.broadcast('test broadcast');40 });41 it('Test unsubscribe', function(done){42 subscription.subscribe('dirwin', () => {43 assert.fail();44 });45 subscription.unsubscribe('dirwin');46 subscription.broadcast('test broadcast');47 setTimeout(function(){48 subscription.unsubscribeAll();49 done();50 }, subscriptionLength * 2);51 });52 it('Test Subscription Length', function(done){53 subscription.subscribe('dirwin', () => {54 assert.fail();55 });56 setTimeout(function(){57 subscription.broadcast('test broadcast');58 }, subscriptionLength);59 setTimeout(function(){60 subscription.unsubscribeAll();61 done();62 }, subscriptionLength * 2);63 });64 it('Too Many Subscriptions', function(done){65 for(var i = 0; i < maxSubscribers+1; ++i){66 subscription.subscribe(i, () => {67 if(i === maxSubscribers){68 assert.fail()69 }70 });71 }72 setTimeout(done, 1000);73 });74 it('Too Many Broadcasts', function(done){75 var value = 0;76 subscription.subscribe('testBroadcastMax', () => {77 if(value === maxBroadcast){78 assert.fail()79 }80 value++;81 });82 for(var i = 0; i < maxBroadcast+1; ++i){83 subscription.broadcast();84 }85 setTimeout(done, 1000);86 });...
Using AI Code Generation
1var channel = pusher.subscribe('my-channel');2channel.bind('my-event', function(data) {3 alert(data.message);4});5var channel = pusher.subscribe('my-channel');6channel.bind('my-event', function(data) {7 alert(data.message);8});9var channel = pusher.subscribe('my-channel');10channel.bind('my-event', function(data) {11 alert(data.message);12});13var channel = pusher.subscribe('my-channel');14channel.bind('my-event', function(data) {15 alert(data.message);16});17var channel = pusher.subscribe('my-channel');18channel.bind('my-event', function(data) {19 alert(data.message);20});21var channel = pusher.subscribe('my-channel');22channel.bind('my-event', function(data) {23 alert(data.message);24});25var channel = pusher.subscribe('my-channel');26channel.bind('my-event', function(data) {27 alert(data.message);28});29var channel = pusher.subscribe('my-channel');30channel.bind('my-event', function(data) {31 alert(data.message);32});33var channel = pusher.subscribe('my-channel');34channel.bind('my-event', function(data) {35 alert(data.message);36});37var channel = pusher.subscribe('my-channel');38channel.bind('my-event', function(data) {39 alert(data.message);40});41var channel = pusher.subscribe('my-channel');42channel.bind('my-event', function(data) {43 alert(data.message);44});45var channel = pusher.subscribe('my-channel');46channel.bind('my-event', function(data) {47 alert(data.message);48});
Using AI Code Generation
1var broadcast = require('broadcast');2broadcast.broadcast('Hello World!');3var broadcast = require('broadcast');4broadcast.broadcast('Hello World!');5var broadcast = require('broadcast');6broadcast.broadcast('Hello World!');7var broadcast = require('broadcast');8broadcast.broadcast('Hello World!');9var broadcast = require('broadcast');10broadcast.broadcast('Hello World!');11var broadcast = require('broadcast');12broadcast.broadcast('Hello World!');13var broadcast = require('broadcast');14broadcast.broadcast('Hello World!');15var broadcast = require('broadcast');16broadcast.broadcast('Hello World!');17var broadcast = require('broadcast');18broadcast.broadcast('Hello World!');19var broadcast = require('broadcast');20broadcast.broadcast('Hello World!');21var broadcast = require('broadcast');22broadcast.broadcast('Hello World!');23var broadcast = require('broadcast');24broadcast.broadcast('Hello World!');25var broadcast = require('broadcast');26broadcast.broadcast('Hello World!');27var broadcast = require('broadcast');28broadcast.broadcast('Hello World!');29var broadcast = require('broadcast');30broadcast.broadcast('Hello World!');
Using AI Code Generation
1var broadcast = require('broadcast');2broadcast.broadcast('my-event', {foo: 'bar'});3var broadcast = require('broadcast');4broadcast.on('my-event', function(data){5 console.log(data);6});7var broadcast = require('broadcast');8broadcast.on('my-event', function(data){9 console.log(data);10});11var broadcast = require('broadcast');12broadcast.on('my-event', function(data){13 console.log(data);14});15var broadcast = require('broadcast');16broadcast.on('my-event', function(data){17 console.log(data);18});19var broadcast = require('broadcast');20broadcast.on('my-event', function(data){21 console.log(data);22});23var broadcast = require('broadcast');24broadcast.on('my-event', function(data){25 console.log(data);26});27var broadcast = require('broadcast');28broadcast.on('my-event', function(data){29 console.log(data);30});31var broadcast = require('broadcast');32broadcast.on('my-event', function(data){33 console.log(data);34});35var broadcast = require('broadcast');36broadcast.on('my-event', function(data){37 console.log(data);38});39var broadcast = require('broadcast');40broadcast.on('my-event', function(data){41 console.log(data);42});43var broadcast = require('broadcast');44broadcast.on('my-event', function(data){45 console.log(data);46});47var broadcast = require('broadcast');48broadcast.on('my-event', function(data){49 console.log(data);50});51var broadcast = require('broadcast');52broadcast.on('my-event', function(data){53 console.log(data);54});55var broadcast = require('broadcast');56broadcast.on('my-event', function(data){57 console.log(data);58});59var broadcast = require('broadcast');60broadcast.on('my-event', function(data){61 console.log(data);62});63var broadcast = require('broadcast');64broadcast.on('my-event', function(data){65 console.log(data);66});67var broadcast = require('broadcast
Using AI Code Generation
1var broadcast = require('broadcast');2});3var broadcast = require('broadcast');4});5var broadcast = require('broadcast');6});7var broadcast = require('broadcast');8});
Using AI Code Generation
1var broadcast = require('./broadcast.js');2broadcast.broadcastMessage('Hello World');3var availableModules = require('./availableModules.js');4var broadcast = function(message) {5 for (var i = 0; i < availableModules.length; i++) {6 var module = require('./' + availableModules[i].name + '.js');7 module.broadcastMessage(message);8 }9}10exports.broadcast = broadcast;11var availableModules = require('./availableModules.js');12var broadcast = function(moduleName, message) {13 for (var i = 0; i < availableModules.length; i++) {14 if (availableModules[i].name == moduleName) {15 var module = require('./' + availableModules[i].name + '.js');16 module.broadcastMessage(message);17 }18 }19}20exports.broadcast = broadcast;21var availableModules = require('./availableModules.js');22var broadcast = function(groupName, message) {23 for (var i = 0; i < availableModules.length; i++) {24 if (availableModules[i].group == groupName) {25 var module = require('./' + availableModules[i].name + '.js');26 module.broadcastMessage(message);27 }28 }29}30exports.broadcast = broadcast;31var availableModules = require('./availableModules.js');32var broadcast = function(groupName, moduleName, message) {33 for (var i = 0; i < availableModules.length; i++) {34 if (availableModules[i].group == groupName && availableModules[i].name == moduleName) {35 var module = require('./' + availableModules[i].name + '.js');36 module.broadcastMessage(message);37 }38 }39}40exports.broadcast = broadcast;41 { name: 'module1', group: 'group1' },42 { name: 'module2', group: 'group1' },43 { name: 'module3', group: 'group2' },44 { name: 'module4', group
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!