Pusher with multi-database companies - javascript

I'm trying to figure out what's the best approach to use Pusher in my application.
My app consists in a normal website where users log in
Each user is connected to a company
Each company has its own database
So, I need my app to allow the send of regular messages and notifications (like popups) to people that only belong to the company signed for.
Doubts/problems:
If I create a channel called notifications and send events to it, all users (no matter what company) will receive it
If I set up some sort of token associated to the company won't work because some notifications/messages should only be sent to a restrict users
In my previous project I associated a token to the company because there was no activity between users only a "show-off" of what PHP was doing (within a loop) in each company, something like:
PHP:
$this->pusher->trigger($company_pusher_token, 'feedback', $data);
JS:
var token = $('#company_pusher_token').val();
var channel = pusher.subscribe(token);
channel.bind('feedback', function(data)
{
alert('working..');
});
So, how should I proceed to accomplish what I'm looking for?

It looks like you are going in the right direction.
The channel name would always start with the company unique prefix. If only a subset of users of that company should be notified then combine the prefix with the group ID.
Channels:
"notifications": send to all companies, all clients
"<company-token>-notifications": send to all clients from a given company
"<company-token>-<group-id>-notifications": send to all clients from a group within a given company
By the way if you want to make sure your customers can't bind to another company's channel make sure either the company token is not guessable, or use the "private-" prefix to let you control the authorization on the subscription of channels.

Related

How to get payout data in Stripe Connect?

I'm adding Stripe Connect to my app to send payments to the users,I need to create a payment dashboard where each user can see all the payments that were sent to them( like payment history). I've read here https://stripe.com/docs/api/payouts/create that I can retrieve list of all payouts or individual payout(by submitting payout id), but I can't find information on how to get payout data per user. For example, if I have user John Doe, I want to get all the payout information for John only with 1 API call, is it possible in Stripe Connect?
If you want to retrieve the list of payments on the connected account, you should retrieve the list of Charges using the stripeAccount header.
You can also make use of the auto-pagination built into Stripe SDKs to loop through the list.
Example
for await (const charge of stripe.charges.list({stripeAccount: 'connected_account_id'})) {
// Do something with charge
console.log(charge);
}
You may want to reach out to Stripe to check if you're eligible to use this beta where you can embed a payments dashboard component into your site : https://stripe.com/docs/connect/get-started-connect-embedded-uis
If I understand goodly your question, you want to retrieve connected accounts' payouts.
You have to know that payout api using is when you move money on your account. In the other hand, if you want to move money to any connected account you need to use transfert api.
Code
You need to know user connect account
const stripe = require('stripe')('API_KEY');
const transfers = await stripe.transfers.list({
destination: 'acct_id.....',
});

Adding attachment/s to PHP Mailer is taking long loading process to complete

Good day everyone, I'm looking for the best way to automate my purchases through my web app. Hope you can give a good advice or suggestion
Let's say we have one product, one user, and order table,
Product name is MusicMixes, product price is 50$, product attachment is Music.mp3 (with a size of 50mb)
User name is Juan and user email is juan#mail.com
Here's the current scenario/application that I have. I am using phpmailer() to send an email with attachments to buyers after a successful purchase using this syntax.
$mail->addAttachment("uploads/".$file_name);
Assuming the buyer bought MusicMixes product, the behavior of the process will be:
The web app will get the User's name and email using session.
The web app will fetch the data of the Product (incl. the Attachment) from database based from the Product ID/Name
The web app will then send an email to the buyer, the structure is like this.
if(isset[post]){
//get user data from session
$name = $session[name]
$email = $session[email]
//get data based on id/name
stmtSelect = select * from product where ID/Name=MusicMixes
//Insert data to db
stmtInsert = insert into order (name,email,productid,productname,productattachment,productprice)
if(stmt execute){
//send email with attachment
$mail->Addattachement....
}
//Process is done
}
I'm encountering a long loading process before the process is submitted/done because I assume that the web app will fetch the attachment (big size, 50mb) and upload into phpmailer() and send it to the user. Typically, if the user refreshes/cancelled/did something to the browser while it is still loading. There are times that the attachments/email are not sent but the data is being saved into the DB.
My question is...
Is it possible like to save it first to database so the loading will be done instantly.
purchase.php = saves data to DB
then after some time maybe after 3-5 minutes, theres something (I can't figure it out) within the web app that will trigger mailSend.php
mailSend.php = sends the email w/ attachments based on DB.
There is somewhat another PHP file/other instances that will handle the mailing process to get the necessary info from the DB then send the email with attachments to the user? so that it doesn't affect the loading time/UX for the user. Something like its loading an email function, the backend is doing its thing without interrupting the user. I can't really explain myself so Hopefully you guys get what I meant.
Thanks for the big help!

Display channel name in featherjs chat client

I am following this repo to build a chat application.
I am trying to show the channel name (the default room which users are logged into) in the chat client.
Is there a way to access channel info from
const client = feathers();
in the file
No, I do not believe so. Channels are a construct on the server side built on top of sockets. Each client are a socket receiving and sending data to the server. On the server sockets/clients can be grouped together into channels, so that you can easily broadcast to many clients, for example:
app.channel('authenticated').send({
warning: "Perimeter has been breached"
});
From what I understand you are trying to create a chat with multiple rooms a.k.a channels. In order to do that you need first to implement the ability to join a specific channel first, this can be done by creating multiple channels on the server, take a look here: https://docs.feathersjs.com/api/channels.html#example
in your src/channels.js:
const { user } = connection;
if (user.room == 'yoyo') {
app.channel('yoyo').join(connection);
}
Then I would recommend to store the room in the user object.
On the client side, when user sign up you could for example do something like this (app.js, line 19):
await client.service('users').create(Object.assign({ room: 'yoyo' }, credentials));
You could get which room to join from the signup form or perhaps from the path.

Send FCM Notification to multiple users using their UID through cloud functions

i have users Uid in an 'users' array as ['uid1','uid2'] now i will be sending notifications to these users in cloud function?
exports.sendNotificationFromCr = functions.database.ref('/cr/{crUid}/notifications/{notificationid}/').onWrite(event => {
const uid = ['uid1','uid2']; // some how i get this.
// some work to send notifications
// to all tokens of uid1 and uid2.
}
here is the database structure:
users/
uid1/
name:{name}
FCM-key/
token1:true
token2:true
uid2/
...
FCM-key/
token3:true
using ['uid1','uid2'] i want to send notification to all 3 tokens in my database. how to do that?
If you're using something like firebase. Then you would want to have a notifications database model, that has the userId, the notification title, body and perhaps image, also a seen flag (true or false).
You would then post notifications either from your clients or from your cloud server code into the database. One per client/notification. If you have thousands of users you would use some sort of server-side cronjob, to offload this so that it runs outside of say your client to server API.
On the clients, you would be listening for new rows in that model filtering on the userId and when they appear, display them to the client in your UI. Once the client has seen the notification you would mark it as seen on the client.
Without knowing what platforms, code base, DB you are using it's impossible to explain in code terms how this would be done.
There are various API's for IOS and Android and Firebase that resolve this.

Sending data only to chosen users using Socket.io-node

Is it possible to send data using socket.io-node just to chosen group of users? For example, how could I implement chat with different rooms? I dont want .broadcast() to send data to all logged in users.
Normally you should have for each room a list of connected user and those user all have a client object that you should have stored somewhere. So when you want to send a message to a specific room, you just have to iterate over the connected user of that room and access their client object and send the data.
In short, it is possible you just have to send your data to each of the users in the group one-by-one.
socket.io has a grouping functionality built in
On the socket object for a single connection, like you get passed when a new user connects, you can call .join('roomName') where roomName is any string you want to use to identify the "room", you could use a room name like "profile/14" to create a channel for updates to user #14's profile.
Then on the main io object do something like:
io.sockets.in('profile/14').emit('newComment', {message:'hello'});
The message will go out to all connections that have .join()'d the given room.
Typically I'll have my client emit a "hello" event onConnect that identifies what content the client is interested in subscribing to, and then on the server side my handler for the "hello" event handles .join()'ing the client into whatever rooms are needed

Categories