'How to get hold messages from socket.io when a socket reconnects
I'm creating a chat function for a flutter application. I'm using socket.io version 2 because it is the stable version with socket.io-client-dart
My chat functions are working fine. My problem is if one person disconnected between a chat and connect back after how he can get the messages that came to him between the disconnection.
here is my code.
const express = require("express");
var http = require("http");
const app = express();
const port = process.env.PORT || 5000;
var server = http.createServer(app);
var io = require("socket.io")(server);
var clients = {};
io.on("connection", (socket) => {
console.log("connetetd");
socket.on("signin", (user_id) => {
clients[user_id] = socket.id;
});
socket.on("message", (msg) => {
console.log("user-messaging")
socket.to(clients[msg.targetId]).emit("message", msg)
});
socket.on("disconnect", () => {
console.log("disconnected from soket - ",socket.id);
});
});
server.listen(port, "0.0.0.0", () => {
console.log("Socket server started in port -" + port);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
