Realtime updates from multiple pusher channels - pusher

I am wondering if it is able to import data from multiple pusher channels.
For one channel you use var channel = pusher.subscribe('channel_name');
I've tried var channel = pusher.subscribe('channel1','channel2'); but that doesn't seem to work.
Looking through the docs I have found nothing about this.
Any help would be gratefully appreciated!

You have to subscribe to each channel individually e.g.
var channel1 = pusher.subscribe( 'channel1' );
var channel2 = pusher.subscribe( 'channel2' );

Related

Discord.js Network Bot : Getting the users message and sending it to the other channels

I am new to coding and I was trying to make a network bot for my server.
I am trying to have it so if someone sends a message in the network channel, then it will send that message to channels. I need some help as I have no idea what I am doing and can't find anything on Google.
I am going to set the time later but I want to get the first bit working.
Here's my code :
const network = "1022619449662124052"
const channels = [`1009882056970485852`, `1009924409714299040`]
const timeout = 1800000 // 30 minutes
client.on("messageCreate", message => {
if(message.channel.(network))// The channel that the bot watchs for a message / advertisement
let network = new Discord.MessageEmbed()
.setTitle(`Grow Togethers NetWork`)
.setDescription(`Ad Posted By : ${message.author.tag}`)
message.channel.get(channels).send(`${content}` {embed: [network]})
//${content} = the message that the user used ( advertisement )
})
Iterate through your array of ids and get the channel object. Then send to that channel
channels.forEach(id => {
const channel = message.guild.channels.cache.get(id);
channel
.send(...)
.catch(console.error);
});

Reading contents of DB for channel.id

My logging command needs a channel to send messages, I do this with a >logging #channel-here command, it stores on better-sqlite3, my issue is I am not sure on how to read the contents and convert it to a channel.
I have been working on this for several days, and I have tried several different things, this was my latest attempt
const id = sql.prepare(`SELECT channel FROM logging WHERE guildid = ${message.guild.id};`).get();
const logs = client.channels.get(id);
if (!logs) return;
logs.send(`A message was deleted`);
const logs = needs to = the channel id that you see in the channel record if the guildid record matches the one that the message was deleted in.
Instead of saving the channels mention you should save the channels id.
<#channel-id> is used to mention a channel, but discord.js <guild>.channels.get(), takes only the ID.
So you should only store the Channel Id in the Database, in your code for >logging #channel-here just use const mentionedchannel = message.mentions.channels.first();
and then into your DB just write the mentionedchannel.id, then your .get() should work!

Get user join / leave events retroactively from Channels

I'm trying to do some analytics on average response time from some of our users on Twilio Chat.
I'm iterating through my channels, and I'm able to pull the info about messages, so I can compare times a message went un-responded to. However, I can't determine which users were in the channel at that time.
Is there anything on the channel that would give me historic member data? Who was in the channel? The channel.messages().list() method is only giving me the text of the messages sent to the channel and who it was by, but the user who may have been in a channel to respond changes throughout a channel's life time.
This is on the backend using the node.js SDK. note: This isn't a complete implementation for what I'm trying to do, but taking it in steps to get access to the information I'd need to do this. Once I have these messages and know which users are supposed to be in a channel at a given time, I can do the analytics to see how long it took for the users I am looking for to respond.
var fs = require('fs');
const Twilio = require('twilio');
const client = new Twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH);
const service = client.chat.services(env.TWILIO_IPM_SERVICE_SID);
async function getChatMessages() {
const fileName = 'fileName.csv';
const getLine = message => {
return `${message.channelSid},${message.sid},${message.dateCreated},${message.from},${message.type},${message.body}\n`;
}
const writeToFile = message => { fs.appendFileSync(fileName, getLine(message)); };
const headerLine = `channelSid,messageSid,dateCreated,author,type,body`;
fs.writeFileSync(fileName, headerLine);
await service.channels.each(
async (channel, done) => {
i++;
let channelSid = channel.sid;
if( channel.messagesCount == 0 ) return;
try {
await channel.messages().list({limit:1000, order:"asc"}).then(
messages => {
messages.forEach( writeToFile );
}
);
} catch(e) {
console.log(`There was an error getting messages for ${channelSid}: ${e.toString()}`);
}
if( i >= max ) done();
}
);
}
I'm beginning to be resigned to the fact that maybe this would only have been possible to track had I set up the proper event listeners / webhooks to begin with. If that's the case, I'd like to know so I can get that set up. But, if there's any endpoint I can reach out to and see who was joining / leaving a channel, that would be ideal for now.
The answer is that unfortunately you can not get this data retroactively. Twilio offers a webhooks API for chat which you can use to track this data yourself as it happens, but if you don't capture the events, you do not get access to them again.

how can I make private chat rooms with sockjs?

I am trying to make a chat system where only two users are able to talk to each other at a time ( much like facebook's chat )
I've tried multiplexing, using mongoDB's _id as the name so every channel is unique.
The problem I'm facing is that I cannot direct a message to a single client connection.
this is the client side code that first sends the message
$scope.sendMessage = function() {
specificChannel.send(message)
$scope.messageText = '';
};
this is the server side receiving the message
specificChannel.on('connection', function (conn) {
conn.on('data', function(message){
conn.write('message')
}
}
When I send a message, to any channel, every channel still receives the message.
How can I make it so that each client only listens to the messages sent to a specific channel?
It appeared that SockJS doesn't support "private" channels. I used the following solution for a similar issue:
var channel_id = 'my-very-private-channel'
var connection = new SockJS('/pubsub', '')
connection.onopen = function(){
connection.send({'method': 'set-channel', 'data': {'channel': channel_id}})
}
Backend solution is specific for every technology stack so I can't give a universal solution here. General idea is the following:
1) Parse the message in "on_message" function to find the requested "method name"
2) If the method is "set-channel" -> set the "self.channel" to this value
3) Broadcast further messages to subscribers with the same channel (I'm using Redis for that, but it also depends on your platform)
Hope it helps!

Pusher binding to events regardless of channel

I am attempting to listen to a particular event type regardless of the channel it was triggered in. My understanding of the docs (http://pusher.com/docs/client_api_guide/client_events#bind-events/lang=js) was that I can do so by calling the bind method on the pusher instance rather than on a channel instance. Here is my code:
var pusher = new Pusher('MYSECRETAPPKEY', {'encrypted':true}); // Replace with your app key
var eventName = 'new-comment';
var callback = function(data) {
// add comment into page
console.log(data);
};
pusher.bind(eventName, callback);
I then used the Event Creator tool in my account portal to generate an event. I used a random channel name, set the Event to "new-comment" and just put in some random piece of text into the Event Data. But, I am getting nothing appearing in my Console.
I am using https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js, and performing this test in the latest Chrome.
What am I missing?
Thanks!
Shaheeb R.
Pusher will only send events to the client if that client has subscribed to the channel. So, the first thing you need to do is subscribe the channel. Binding to the event on the client:
pusher.bind('event_name', function( data ) {
// handle update
} );
This is also known as "global event binding".
I've tested this using this code and it does work:
http://jsbin.com/AROvEDO/1/edit
For completeness, here's the code:
var pusher = new Pusher('APP_KEY');
var channel = pusher.subscribe('test_channel');
pusher.bind('my_event', function(data) {
alert(data.message);
});

Resources