Instant messager in ionic

Hello,

I would like to know if there are example of library that can be used in ionic to build a chat system so that 2 users can communicate, no groups nothing to complex.

Thanks!!
Sophian.

Yep, I’d check out this tutorial

http://melvin0008.github.io/blog/ionic-socketio-chat/

the magic word is “socket” ^^. SocketIO is a framework to work with websockets in nodejs. There is the server-side part something like a “socket-server” with manages all clients and their communication. And the client, which registeres at the server --> listens on what the server sais (without sending requests) or calls the server to do something.

Maybe it is easier to understand:
the ordinary web is something like a Pull-System --> you (the client) sends requests to get new information (it pulls from the backend)
with sockets you have a push-system --> there is no need to send (maybe useless) requests --> the server informs you about new stuff.

1 Like

Perfectly said, @bengtler :smile:

So in the context of a messaging app what you would have is:

  • Server which would handle connections/requests
  • Client App

When User A starts a chat with User B, they both connect to the server. If User A sends a message, it would be a request to the server, which would take in that message and emit it to the other Users in the chat (User B). User B is listening for any incoming requests from the server (done with the open socket), so once a message is emitted from the server, User B’s app will receive this message and display it.

Somethings that you need to consider are sockets are only active when a user is using the app. So if a user closes the app, they won’t receive the message. What you can do is listen for connection closes/disconnects, and when that occurs you could save the message to a queue or DB, and once a user connects, you can grab those saved messages and send them to the user.