About the chat application using Socket.io

Asked 1 years ago, Updated 1 years ago, 105 views

I am creating a chat app like LINE with the html5 hybrid app (I use Monaca).

I plan to use node.js, socket.io, and redis.

"When you search ""socket.io Chat"" on Google, you can find code that acts as a chat on various sites, but all of them are ""what you enter is sent directly to the person you are communicating with in real time."""

"What I want to do is ""The sender saves the input in redis → The recipient pop the data stored in redis with his user ID, and the pop data is deleted from redis."""
In other words, instead of "the sender and receiver are communicating with socket.io in real time," I think it will be a program called "the receiver is communicating with redis in real time."

As I have written so far, I have only been doing PHP, and I am a beginner at node.js, so I have a question.

Is this possible?If possible, I would like you to introduce me to a website that I can refer to.

Is there a more common way?

node.js socket.io redis

2022-09-29 22:07

3 Answers

"I know this area somehow, but I thought that what I want to realize in the question content is the so-called ""Pub/Sub messaging model."""
Note: Pub/Sub messaging model

I'll paste some of the results of my search on socket.io pubsub as a trial.
Now I'm going to summarize about Socket.io
Deliver real-time messages on Socket.io+Redis PubSub
socket.io-redis (GitHub)

"By the way, Redis seems to have a ""pub/sub"" function.
Try Redis pub/sub in 3 minutes


2022-09-29 22:07

I've never used Monaca and Socket.io, so I don't know if it's possible, but I thought about how to implement it.I hope it will be helpful.

First, if Client 1 receives a message from another client 2 when it is not connected, it saves the message in the unread stack or queue of redis.
Then, the next time Client 2 connects to Socket, if there is an unread message in redis, it sends it to Client 2 and delete it.If you want to check the message before establishing a connection with Socket, client 2 sends an unread request to the server and receives the unread message in rest.
If clients 1 and 2 are online, broadcast the message without saving it to redis.


2022-09-29 22:07

A helpful framework

You can see what existing web applications, frameworks, services, etc. are using.
In particular, many people use the framework, so it is easy to get a way to scale it and talk about their experiences.
Here are three frameworks that focus on real-time:
(I've never used anything other than Meteor, so I just looked it up.)

  • Meteor
    • MongoDB
    • SockJS
  • DerbyJS
    • MongoDB (replaceable)
    • Redis
    • Racer
      • ShareJS
  • Sails
    • Socket.IO
    • Waterline (ORM with various databases)
  • MongoDB
  • SockJS
  • MongoDB (replaceable)
  • Redis
  • Racer
    • ShareJS
  • ShareJS
  • Socket.IO
  • Waterline (ORM with various databases)

Socket.IO

Socket.IO is used in Sails among the above.
However, Meteor and DerbyJS avoid Socket.IO and use their own library (which can be used alone).
DerbyJS (ShareJS) cites that the order in which messages arrive is not guaranteed as a reason to avoid Socket.IO.

Redis

Redis is used in DerbyJS.
Sails developers also seem to like Redis when they read the old article.
DerbyJS needs Redis as well as MongoDB

If the code for synchronizing clients with each other is put into the application (node.js server), the server process cannot be increased and scaled.
Consequently, the application needs to receive an event from the database when the data is changed.
It requires the PubSub functionality of the database, and MongoDB is limited to that, and Redis is suitable.
(Old FAQ)

It is stated that
You may need to be careful because the questioner doesn't seem to expect a database to be needed around here.

Can Socket.IO and Redis be combined

For the time being, it doesn't seem to be considered a minor or a bad choice in general.
However, if the purpose is not to gain experience, but to complete the application, you can use the framework mentioned above.
If you want to be free from server care, you can use BaaS like Firebase.


2022-09-29 22:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.