0

I would like to create a one-to-one iOS chat app using Firebase. The only examples I found are for group chat implementations and I am not sure how to structure the database for a one-to-one chat.

My main question is: How do I listen for new chats/messages? Let's say user1 wants to chat with user2 and they have not talked before. How does user1 send a message to user2?

Could someone point me in the right direction for implementing a one-to-one chat with Firebase?

Thanks!

Community
  • 1
  • 1
Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60
  • That is an incredibly broad topic. But if you're wondering how to get the users into the same chat room, see http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Jan 19 '17 at 22:20
  • Thank you, that did clear things up a bit! But how do I let user2 know that user1 sent him a message? What databass changes should user2 be observing? – Balázs Vincze Jan 19 '17 at 22:24

1 Answers1

2

Here's a very high level structure to start.

uid_0 will be observing their incoming_messages node, and uid_1 will be observing their incoming_messages node.

uid_1 sends a message to uid_0, that message will have a key created with childByAutoId which will make it a unique message. uid_0 reads it and responds by posting a message to uid_1 incoming_messages node.

This is a basic example and there are 1000 different ways to do it so once you get this working, you can explore the space with other structures depending on your use case.

uid_0
   incoming_messages
    -Y999nsjnss8s
      msg: "hello to uid_0 from uid_1"
      from: "uid_1"

uid_1
   incoming_messages
    -Yu99jis9jms
      msg: "this is uid_0 responding to uid_1 message"
      from: "uid_0
Jay
  • 34,438
  • 18
  • 52
  • 81
  • How do u make paged messages queries with this structure? – Daniel Birowsky Popeski Dec 24 '17 at 23:52
  • @Birowsky Are you asking how to load a certain number of messages? i.e. load the last 5 messages or the last 10 messages? – Jay Dec 25 '17 at 14:33
  • Yeah, as in a regular chat app. And then the previous page of 10, and so on. – Daniel Birowsky Popeski Dec 25 '17 at 14:40
  • @Birowsky There are a *lot* of questions/answers about how to do pagination on this site. From the [docs]() *You can use queryStartingAtValue, queryEndingAtValue, and queryEqualToValue to choose arbitrary starting, ending, and equivalence points for queries. This can be useful for paginating data or finding items with children that have a specific value.* Try searching here for "[firebase]pagination" since this topic has been discussed regularly before. If you have a specific question that has not been answered, consider posting it so we can take a look. – Jay Dec 26 '17 at 13:21
  • Thanx Jay, but this is not a generic question about queries. I'm asking how to query pages of messages specifically for the chat structure you proposed. Reason being, the messages are in different parents, ergo, we can't just take 5 from the messages for user A and 5 from the messages for user B, they just won't match. – Daniel Birowsky Popeski Dec 26 '17 at 14:54
  • @Birowsky If you take a look at the original question, it's a 1-1 chat app listening for new messages only; no limit or amount. So the messages for a particular user will always appear in their incoming_messages node. For that node, uid_0, you could create a query for the last 10 messages from uid_1 or a more generic with a query for the last 10 messages from any user. There's a number solutions that could be implemented but the comments section here isn't a good place for code. If you want to explore some answers, post a question with your structure and specific needs and we'll take a look. – Jay Dec 26 '17 at 18:40
  • @Birowsky Oh... and take a look at [this question](https://stackoverflow.com/questions/37849631/pagination-infinite-scroll-with-firebase) as it will provide one option. You can also search on "[Firebase][swift]infinite scroll" for some other options as well. – Jay Dec 26 '17 at 18:59