Ask coding questions

← Back to all posts
Online Or Not
RYANTADIPARTHI (6046)

Hi,

when you make a repl as a collaboration or team repl, it has a chat feature. In that chat feature, it shows if the user is available(online) or left(offline). I want to know how to do this.

the way replit does it is it detects if the user has closed the repl tab, or still has it on. I would like to know how to do this.

I don't know which languages can do this functionality, but whatever languages you can do it in. I would prefer it to be either: JS or Django.

Thanks!

Comments
hotnewtop
JakeHu2020 (34)

if you use node, socket.io lets you do this sort of thing:

io.on('connection',(socket)=>{
  socket.on('disconnect',()=>{console.log('user closed page')});
});
ch1ck3n (2388)

https://github.com/django/channels and i think websocket has the ability to know when a user has disconnected

RYANTADIPARTHI (6046)

@ch1ck3n i saw this before, but the thing is, how do I use it?

RYANTADIPARTHI (6046)

@ch1ck3n that's not what i meant. I meant, how do i use to check if user Is online or not.

Coder100 (18930)

well
both technically, but not exactly.

I don't believe django does have the option to do websockets, but I do know flask (similar to django) does.

JavaScript has socket.io, so maybe that's something for you there.

But you are kind of overthinking this. Once you implement accounts, you don't need to know if the user closed the tab or not, when they connect to the websocket, all information is given!

RYANTADIPARTHI (6046)

@Coder100 by accounts, do you mean logging in and logging out?

If yes, then that wouldn't work. For now, I got how to check if a user is online, but when the user closes the tab without logging out, it still marks the user as online, but i want to changed to offline. So in replit, it works.

I am asking how to do that.

But if you want an alternate, i guess you could also detect if the user has closed the tab. so that way, you could mark the user as offline.

But do you have any ideas?

Coder100 (18930)

@RYANTADIPARTHI

  1. there is a method called socket.disconnect that tells you
  2. why would users not work? You could just do like a socket.emit with all the user data you have stored in the cookie -- if you could display the username, that will suffice for our socket.emit, the backend can do the rest.
RYANTADIPARTHI (6046)

@Coder100 could you show me how to do this?

Thanks!

Coder100 (18930)

do what part?
as you may know idk the language you have decided to use @RYANTADIPARTHI

ruiwenge2 (1209)

Wow 2 great legends

RYANTADIPARTHI (6046)

@Coder100 i don't really have an option for which language. You can choose whatever is ideal for you. But could you try doing it in django and js? I'll choose which is best. But it's your choice though. Whichever you may like.

Aso for the part, I don't really know which works properly, so please choose the best one.

Thanks!

Coder100 (18930)

sure i'll try implementing it in js @RYANTADIPARTHI

RYANTADIPARTHI (6046)

@Coder100 ok. Please let me know when you are done. Thanks!

RYANTADIPARTHI (6046)

@Coder100 hi, this is kind of good. But can you do it like It in one file? with js in a HTML file? or is there an easier way?

Coder100 (18930)

well yeah you could just embed it in one file

<script>
// your js code
</script>

but for larger projects it is impractical
@RYANTADIPARTHI

RYANTADIPARTHI (6046)

@Coder100 yes, but this is node.js, i don't know how to include all of this in django, also, I want all the left and joined in one page instead of two. So could you please show me how? Thanks!

Coder100 (18930)
  1. node.js is not django, they can't interoperate
  2. what do you mean? It is all in one website, the logging in part is required
    EDIT: it has to be in 2 pages because you are only one person and you won't be able to see the logs clearly (2 people and more can) @RYANTADIPARTHI