How to have one to one chat using firebase in AngularJS - javascript

I am trying to have a one to one chat feature using firebase. Right now, my structure of messages makes sense in the fb database but I am not able to implement one to one chat feature using firebase's api in angular. I am able to spam a chat room where all the users can see everyone's messages but i would like to have a one to one chat feature in my application. My structure is good in the firebase database and i just need to manipulate my javascript code to make it work. My code is as follows.
function chatController($scope,$firebaseArray) {
var ref1=firebase.database().ref().child('users/59c1c11cec/chats/-KuRshPYbO_d9B');
$scope.chats=$firebaseArray(ref1);
var ref=firebase.database().ref().child('chats/-KuRshPYbO_d9B/messages');
$scope.messages=$firebaseArray(ref);
$scope.send=function(){
$scope.messages.$add({
message:$scope.messageText,
// date:Date.now()
})
}
}
I am able to create a one to one chat between two users by hard coding their user ids, but I was wondering if firebase's api is able to do this dynamically. If yes, how would I do it? For reference, 59c1c11cec is the current logged in user id, -KuRshPYbO_d9B is the other person's id who the current person is starting a conversation with which I don't have any access to in my own database but only in firebase's database. And using that users id I retrieve the messages between the two users. How will I be able to achieve this? I just need to replace the hard coded values with the values I talked about.Thanks a lot in advance!

Related

Web Push Same Subscriber Different Endpoint Issue

I am trying to implement the web-push service in one of my projects. So, I actually need to know the actual subscriber numbers in my app. So what problem I am facing is, the service worker generates new endpoints for the same user and I didn't find any unique thing by which I can figure out if the subscriber exists or not.
All my users are also guest users so I don't have any unique thing. There is also an event called onPushSubscriptionChange which doesn't also work in other browsers except for Mozilla.
So is there any way to uniquely identify a user? Like if multiple endpoints are created for the same user then I want to replace the old endpoint with the new one in my database. I am not willing to use cookies also.
Another thing is does firebase cloud messaging solves this issue?
Thank you

subscribe and unsubscribe to topic - firebase

I am developing a website in HTML, JS, and CSS. My result is a PWA (Progressive Web App). It works really great. I am hosting and serving it via the Firebase Hosting and enjoy many functions of firebase. To let the user feel the full power of a PWA I need to manage Push Notifications. For that, I want to use Firebase Cloud Messaging. I already know how to send push notifications and so on. On my webpage, the users can subscribe to topics. And here we go. I do not know how to subscribe/unsubscribe a user to a topic via the javascript without the admins SDK. Can please provide somebody a clear and simple example for subscribe/unsubscribe users? From the documentation, I will not be smart.
Thanks in advance,
Filip.
I would do it like that:
(I assume you have users tokens stored somwhere in the database ordered by users id)
Create table 'topics' - store users ids there.
Create form - let the user add its id to the 'topic' table.
Then, before sending FCM, store every token, from owners which ids are assigned to the specific topic in the 'topic' table.

Firebase one to one chat implementation for web

I implemented friendly chat. But that is group chat. I am new to firebase. Need docs for creating one to one web chat. How to get list of user accounts and selecting single user to send message? Please help me
If the users communicating have user IDs uid_1 and uid_2.
The database structure should be:
contacts
--uid1
--uid2
--uid3
-- so on (1)
--uid2
--uid1
--uid4
-- so on (2)
messages
--uid1
--uid2
-- Push messages here.(3)
--uid2
--uid3
-- Push messages here also.(4)
Adding Contacts
If USER 1 knows USER 2 you can add USER 2 in location (1) and add USER 1 in location (2). Since you are implementing with user IDs the USERS will be able to send messages if they know the other USER's uid. You can send Chat Invites to exchange uids.
Adding Messages
If USER 1 or USER 2 sends a message add at locations (3) and (4). This implementation will make sure that even if USER 1 deletes a conversation with USER 2, USER 2 will still have a copy and thus ensuring privacy.
These are the key elements to make a basic one-on-one Firebase Chat App.
NOTE: User IDs are provided by Firebase on successful authentication.
Maybe try breaking your problem into individual milestones and learn how Firebase works as you need help in each milestone.
According to what you have written, you have to:
Authenticate Users (https://firebase.google.com/docs/auth/)
Save those user profiles onto the Real-Time Firebase Database in the following data structure (https://firebase.google.com/docs/database/)
List those users in a List / RecycleView (learnhowtoprogram.com/android/data-persistence/firebase-recycleradapter)
Manage the conversation through listeners in the adapter that reference child nodes which serve as rooms between 2 people.
Think about how to make your structure as efficient as possible such as not saving conversations twice under each user but rather saving a reference to a separate 'conversations' node.
I recently implemented this in my location-based app (Occupapp) if you want a live example. You will have to register, add a service and log in from another device and select that service to chat to the owner.

Using MongoDB to separate different user data

I'm already familiar that MongoDB is based on documents that are in JSON format. I'm creating my first web app using the MEAN stack, where users can register and then be able to sign in to a back-end dashboard where they can implement products/profile information etc. How would I set that up to be stored on MongoDB? would each user be stored as a document? And as far as security reasons, how can I go about not allowing a GET request to be able to get a different users information?
Currently, I just have a collection of users, and a collection of products (with the unique id number for each user), etc. to me that doesn't seem the proper way to store data.
If anyone can help me on how to setup the database for Mongo that would be fantastic! Thanks in advance!
would each user be stored as a document?
Yes, each user is an object, thus it's stored as a separate document.
how can I go about not allowing a GET request to be able to get a different users information?
This has nothing to do with Mongo or any other data storage mechanism. You'll need to work on a Web service layer which exposes your data filtering requests to authorize them based on user role, claims or any authorization approach you might find useful in your scenario.
Maybe you should look at implementing OAuth2. There's a package that integrates with Express to implement your own OAuth2 authorization server: node-oauth2-server.
Currently, I just have a collection of users, and a collection of
products (with the unique id number for each user), etc. to me that
doesn't seem the proper way to store data.
You are on the right way actually. Now when you show products page for users you need to retrieve only documents that belong to that single user that is currently authenticated, this implies that you have authentication set up, and products have a userId field.
This is how most application work. If you want to read about other ways of solving this then read about multi-tenancy.

Order Facebook highscores by country

I'm working on a Facebook game where scores get saved using the Scores API, while this all works fine, our client wants to be able to filter the highscores by country, I'm wondering if there's an easy way to do this.
When returning the scores, the user object only contains the ID and the name, it would be pretty stupid to get the country fo each user.
Easier way would be to get user country on application install time.
You can do this by adding user location permission to authentication dialog which appears first time
when the user visits your app.
See user_hometown and user_location here.
https://developers.facebook.com/docs/reference/api/user/
Then you can easily look all the user countries from your database, and sort according to country.

Categories