Node js - Creating persistent private chat rooms
Asked Answered
U

2

5

I've been reading so much a bout node js lately, and the chat capabilities seem very nice. However, the only chat examples I've seen basically broadcast a chat server to a fixed URL (like a meeting room). Is it possible to use node js in part to create a chat client more like gchat? - where a chat window is popped up on the current page and then persists through multiple pages. Has anyone seen an example of this yet?

If not, suggestions for other technologies to use for this purpose (I know that's been answered in other questions)?

Thanks.

Univalence answered 11/4, 2011 at 12:57 Comment(4)
It shouldn't be too hard to write. What exactly are you looking for?Dashiell
I'm interested in writing a client to integrate into my website. Users would be able to see their "buddies" and it would have a similar feel to gchat, persisting across all pages if they want. I guess I'm just wondering if it's possible to broadcast multiple private chat sessions to specific users (different from a chat room), and persist that for the user until they close it. I know there are other languages to write this in, but node seemed like a cool one to use if possible. Thanks for the comment.Univalence
By "gchat" you mean google chat or something else?Clyte
Correct, by gchat I'm referring to google chat within gmail.Univalence
D
6

I'll give you a pseudo implementation relying on jquery and now to abstract away tedious IO and tedious DOM manipulation from the solution.

// Server

var nowjs = require('now');
var everyone = nowjs.initialize(httpServer);

everyone.now.joinRoom = function(room) {
    nowjs.getGroup(room).addUser(this.user.clientId);
}

everyone.now.leaveRoom = function(room) {
    nowjs.getGroup(room).removeUser(this.user.clientId);
}

everyone.now.messageRoom = function(room, message) {
    nowjs.getGroup(room).now.message(message);
}

// Client

var currRoom = "";

$(".join").click(function() {
    currRoom = ...
    now.joinRoom(currRoom);
});

$(".send").click(function() {
    var input = ...
    now.messageRoom(currRoom, input.text());
});

now.messageRoom = function(message) {
    $("messages").append($("<div></div>").text(message));
};

I only just noticed myself that the new version of nowjs (0.5) has the group system in build. This basically does what you want for you. No hassle.

If you want you can remove the nowjs dependency and replace it with 100/200 lines of code. I'll leave that as an exercise for the user.

Dashiell answered 11/4, 2011 at 15:37 Comment(2)
Thanks Raynos. I just looked into nowjs and it might be just what I'm looking for. I'll take a stab at it, I believe I should be able to generate groups on demand for private chats or group meetings, and just remove the users and the group when it's finished.Univalence
@Univalence pretty much. Using now is great in terms of abstracting real time IO. You have to watch out you don't go overboard though, you sacrifice efficiency.Dashiell
N
1

Take a look at AjaxIM: https://github.com/freq32/AjaxIM

This is a facebook-style chat application (think friends list, small persistent chat bar at the bottom of the screen, popup chats) based on nodejs.

Nazarene answered 29/7, 2011 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.