Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.on('receiveMessage',(data)=>print("data")) called twice #33

Closed
jabonge opened this issue Sep 18, 2019 · 9 comments
Closed

socket.on('receiveMessage',(data)=>print("data")) called twice #33

jabonge opened this issue Sep 18, 2019 · 9 comments

Comments

@jabonge
Copy link

jabonge commented Sep 18, 2019

socket = IO.io("myserverurl", <String, dynamic>{
'transports': ['websocket']
});
socket.nsp = "/chats";

    socket.on("receiveMessage", (data) {
      print("from socket $data");
             });
    socket.connect();
    socket.emit("join", {"Id": "id"});

i send message my server log message call twice why it is called twice?
server code
io.of('nsp').to('id').('receiveMessage',message);

@jabonge jabonge changed the title socket.on('event',()=>print) socket.on('receiceMessage',(data)=>print("data")) called twice Sep 18, 2019
@jabonge jabonge changed the title socket.on('receiceMessage',(data)=>print("data")) called twice socket.on('receiveMessage',(data)=>print("data")) called twice Sep 18, 2019
@jabonge
Copy link
Author

jabonge commented Sep 18, 2019

in Flutter i dont really know why log called twice i will wait answer thank you for your attention

@bastibense
Copy link

Came across the same issue. Apparently calling .connect() causes all event handlers to get registered/fired twice.

Try removing the connect() call, the socket should automatically connect.

@leonidl
Copy link

leonidl commented Jan 18, 2020

You can also add "'autoConnect': false" option to optional parameters list to stop socket from auto-connecting. In that case you will have to call connect() manually.

@bastibense
Copy link

Still, shouldn't this at least print out a warning to the log or just ignore a second call to connect()?

@jumperchen
Copy link
Member

@bastibense I reopen it, and it looks like the behavior is not the same as JS version.

@itaispector
Copy link

Came across the same issue. Apparently calling .connect() causes all event handlers to get registered/fired twice.

Try removing the connect() call, the socket should automatically connect.

still fired twice

@RIddhiIroid
Copy link

Came across the same issue. Apparently calling .connect() causes all event handlers to get registered/fired twice.
Try removing the connect() call, the socket should automatically connect.

still fired twice

Hello
Did you manage to solve this?

@AkshayJainX
Copy link

Facing the same issue flutter...

@AkshayJainX
Copy link

front end ----

IO.Socket? socket;
@OverRide
void initState() {
super.initState();
// connect();

focusNode.addListener(() {
  if (focusNode.hasFocus) {
    setState(() {
      show = false;
    });
  }
});
connect();

}

void connect() {
// MessageModel messageModel = MessageModel(sourceId: widget.sourceChat.id.toString(),targetId: );
socket = IO.io("https://studiovity.com/", <String, dynamic>{
"transports": ["websocket"],
// "autoConnect": true,
});
// socket!.connect();
socket!.emit("signin", widget.sourchat.id);
socket!.onConnect((data) {
print("Connected");
socket!.on("message", (msg) {
print(msg);
setMessage("destination", msg["message"]);
_scrollController.animateTo(_scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 300), curve: Curves.easeOut);
});
});
print(socket!.connected);
}

Backend

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);

//middlewre
app.use(express.json());
var clients = {};

io.on("connection", (socket) => {
console.log("connetetd");
// accept();
console.log(socket.id, "has joined");
socket.on("signin", (id) => {
console.log(id);
clients[id] = socket;
console.log(clients);
});
socket.on("message", (msg) => {
console.log(msg);
let targetId = msg.targetId;
if (clients[targetId]) clients[targetId].emit("message", msg);
});
});

server.listen(port, "0.0.0.0", () => {
console.log("server started");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants