socket.io leave current room and join after button is clicked









up vote
-1
down vote

favorite












my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle



Heading



so i tried this code



client-side:



 N.addEventListener('click', function(e)
socket.emit('next')

);

socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);


server-side:



 socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);


but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that



 var room = "room" + numb;
socket.current_room = room;


but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3










share|improve this question























  • The usual way you would leave one room and join another is to send your server a message asking it to do a socket.leave(oldRoom) and socket.join(newRoom) on your behalf. There is no need to drop the connection and reconnect just to change rooms.
    – jfriend00
    Nov 8 at 3:30










  • can u show me an example i get a bit confused , because i think i am doing what u are saying
    – codR
    Nov 8 at 11:47






  • 2




    Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
    – Glorfindel
    2 days ago










  • i tried to edit but it was impossible
    – codR
    2 days ago














up vote
-1
down vote

favorite












my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle



Heading



so i tried this code



client-side:



 N.addEventListener('click', function(e)
socket.emit('next')

);

socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);


server-side:



 socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);


but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that



 var room = "room" + numb;
socket.current_room = room;


but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3










share|improve this question























  • The usual way you would leave one room and join another is to send your server a message asking it to do a socket.leave(oldRoom) and socket.join(newRoom) on your behalf. There is no need to drop the connection and reconnect just to change rooms.
    – jfriend00
    Nov 8 at 3:30










  • can u show me an example i get a bit confused , because i think i am doing what u are saying
    – codR
    Nov 8 at 11:47






  • 2




    Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
    – Glorfindel
    2 days ago










  • i tried to edit but it was impossible
    – codR
    2 days ago












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle



Heading



so i tried this code



client-side:



 N.addEventListener('click', function(e)
socket.emit('next')

);

socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);


server-side:



 socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);


but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that



 var room = "room" + numb;
socket.current_room = room;


but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3










share|improve this question















my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle



Heading



so i tried this code



client-side:



 N.addEventListener('click', function(e)
socket.emit('next')

);

socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);


server-side:



 socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);


but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that



 var room = "room" + numb;
socket.current_room = room;


but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3







node.js sockets express socket.io chatroom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Glorfindel

16.3k114769




16.3k114769










asked Nov 7 at 19:47









codR

85




85











  • The usual way you would leave one room and join another is to send your server a message asking it to do a socket.leave(oldRoom) and socket.join(newRoom) on your behalf. There is no need to drop the connection and reconnect just to change rooms.
    – jfriend00
    Nov 8 at 3:30










  • can u show me an example i get a bit confused , because i think i am doing what u are saying
    – codR
    Nov 8 at 11:47






  • 2




    Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
    – Glorfindel
    2 days ago










  • i tried to edit but it was impossible
    – codR
    2 days ago
















  • The usual way you would leave one room and join another is to send your server a message asking it to do a socket.leave(oldRoom) and socket.join(newRoom) on your behalf. There is no need to drop the connection and reconnect just to change rooms.
    – jfriend00
    Nov 8 at 3:30










  • can u show me an example i get a bit confused , because i think i am doing what u are saying
    – codR
    Nov 8 at 11:47






  • 2




    Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
    – Glorfindel
    2 days ago










  • i tried to edit but it was impossible
    – codR
    2 days ago















The usual way you would leave one room and join another is to send your server a message asking it to do a socket.leave(oldRoom) and socket.join(newRoom) on your behalf. There is no need to drop the connection and reconnect just to change rooms.
– jfriend00
Nov 8 at 3:30




The usual way you would leave one room and join another is to send your server a message asking it to do a socket.leave(oldRoom) and socket.join(newRoom) on your behalf. There is no need to drop the connection and reconnect just to change rooms.
– jfriend00
Nov 8 at 3:30












can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47




can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47




2




2




Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago




Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago












i tried to edit but it was impossible
– codR
2 days ago




i tried to edit but it was impossible
– codR
2 days ago












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.



Here's your sequence of events:



  1. User clicks button

  2. Client code does socket.emit('next')

  3. Server receives next message

  4. Server calls socket.leave(socket.current_room) to leave the current room

  5. Server does a .emit('next') to everyone still in that room

  6. Server calls socket.join(room) (I have no idea where the room variable comes from)

  7. Clients receive next message and that causes them to socket.disconnect() and thensocket.connect()` (no idea why it's doing that).

The things that seem odd to me about this sequence of events are:



  1. The socket.disconnect() followed by the socket.connect() is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.


  2. I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.


  3. Please don't use the same message name next to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.






share|improve this answer




















  • check my ANSWER there is the code because i can not post it in coms
    – codR
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196749%2fsocket-io-leave-current-room-and-join-after-button-is-clicked%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.



Here's your sequence of events:



  1. User clicks button

  2. Client code does socket.emit('next')

  3. Server receives next message

  4. Server calls socket.leave(socket.current_room) to leave the current room

  5. Server does a .emit('next') to everyone still in that room

  6. Server calls socket.join(room) (I have no idea where the room variable comes from)

  7. Clients receive next message and that causes them to socket.disconnect() and thensocket.connect()` (no idea why it's doing that).

The things that seem odd to me about this sequence of events are:



  1. The socket.disconnect() followed by the socket.connect() is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.


  2. I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.


  3. Please don't use the same message name next to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.






share|improve this answer




















  • check my ANSWER there is the code because i can not post it in coms
    – codR
    2 days ago














up vote
1
down vote



accepted










There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.



Here's your sequence of events:



  1. User clicks button

  2. Client code does socket.emit('next')

  3. Server receives next message

  4. Server calls socket.leave(socket.current_room) to leave the current room

  5. Server does a .emit('next') to everyone still in that room

  6. Server calls socket.join(room) (I have no idea where the room variable comes from)

  7. Clients receive next message and that causes them to socket.disconnect() and thensocket.connect()` (no idea why it's doing that).

The things that seem odd to me about this sequence of events are:



  1. The socket.disconnect() followed by the socket.connect() is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.


  2. I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.


  3. Please don't use the same message name next to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.






share|improve this answer




















  • check my ANSWER there is the code because i can not post it in coms
    – codR
    2 days ago












up vote
1
down vote



accepted







up vote
1
down vote



accepted






There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.



Here's your sequence of events:



  1. User clicks button

  2. Client code does socket.emit('next')

  3. Server receives next message

  4. Server calls socket.leave(socket.current_room) to leave the current room

  5. Server does a .emit('next') to everyone still in that room

  6. Server calls socket.join(room) (I have no idea where the room variable comes from)

  7. Clients receive next message and that causes them to socket.disconnect() and thensocket.connect()` (no idea why it's doing that).

The things that seem odd to me about this sequence of events are:



  1. The socket.disconnect() followed by the socket.connect() is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.


  2. I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.


  3. Please don't use the same message name next to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.






share|improve this answer












There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.



Here's your sequence of events:



  1. User clicks button

  2. Client code does socket.emit('next')

  3. Server receives next message

  4. Server calls socket.leave(socket.current_room) to leave the current room

  5. Server does a .emit('next') to everyone still in that room

  6. Server calls socket.join(room) (I have no idea where the room variable comes from)

  7. Clients receive next message and that causes them to socket.disconnect() and thensocket.connect()` (no idea why it's doing that).

The things that seem odd to me about this sequence of events are:



  1. The socket.disconnect() followed by the socket.connect() is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.


  2. I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.


  3. Please don't use the same message name next to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 21:58









jfriend00

420k46531577




420k46531577











  • check my ANSWER there is the code because i can not post it in coms
    – codR
    2 days ago
















  • check my ANSWER there is the code because i can not post it in coms
    – codR
    2 days ago















check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago




check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196749%2fsocket-io-leave-current-room-and-join-after-button-is-clicked%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20