Nodejs private chat using php

I want to create a chat system in nodeJs + MYSQL using php. This will be a private one-to-one chat and will save the chat to the database. Does anyone know where I need to start from.

Currently I got this code for SERVER:

var app = require('express').createServer()
var io = require('').listen(app);


// routing
app.get('/', function (req, res) {
 res.sendfile(__dirname + '/index.html');

// usernames which are currently connected to the chat
 var usernames = {};

 io.sockets.on('connection', function (socket) {

// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
    // we tell the client to execute 'updatechat' with 2 parameters
    io.sockets.emit('updatechat', socket.username, data);

// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client username to the global list
    usernames[username] = username;
    // echo to client they've connected
    socket.emit('updatechat', 'SERVER', 'you have connected');
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
    // update the list of users in chat, client-side
    io.sockets.emit('updateusers', usernames);

// when the user disconnects.. perform this
socket.on('disconnect', function(){
    // remove the username from global usernames list
    delete usernames[socket.username];
    // update list of users in chat, client-side
    io.sockets.emit('updateusers', usernames);
    // echo globally that this client has left
    socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has         disconnected');



source to share

1 answer

There are two ways. Thirsty you can store references to all sockets in an array (all, at least the IDs of those sockets). When a user sends a private message, you look at the array for the target socket and send it to that specific one. This requires a specific socket identifier. You can use internal

, but it will be a problem when the client reconnects (a new ID is generated). And there is another problem when your application is running on more than one machine (they cannot share arrays of connected clients).

The second way is to use rooms. Whenever a client connects, I will assume that they have a name like John. Then you can use something like this for your connection:



Now this creates a room and adds to it socket

. If you want to send a message to John, you simply use'/priv/John').emit('msg', data);


At this point, you can be sure that the message is being sent exactly to the socket in the /priv/John

room. This works great with Redis in combination with (to avoid a lot of machine issues) and session authorization. I haven't tried it with memoryStore, but it should work as well.

Also you don't have to worry about rooms when clients disconnect. destroys empty rooms automatically.



All Articles