I’m currently learning node.js by developing a small chat application over TCP (classic case).
What I would like to do now is to extend this application to use static users:
- All logic around messaging should be based on users rather than
sessions. - Each session therefore needs to be saved along with a
reference to the user (so that the application can send messages in
the correct session based on user name). -
I have looked into Redis to store this data but not been successful so far.
tcpServer.on 'connection', (tcpSocket) -> tcpSocket.write "Welcome" redis.hmset("sessions", userid, tcpSocket) tcpSocket.on "data", (data) -> redis.hvals "sessions", (err, repl) -> repl.forEach (reply, y) -> reply.Write("Data is written to clients!") tcpServer.listen 7000
Current error message: TypeError: Object [object Object] has no method ‘write’
This indicates that I cannot store a tcpSocket as I’m trying to do right now. Can someone tell me if I can make minor adjustments to make this right or should I rethinkt this solution?
Redis supports very few data types: string, lists, sets, sorted lists and hash tables. That’s it. Some other types, like Date, can be easily converted to/from string, so it can be used too. But socket is a complex internal object, so you most probably can’t convert it into string and therefore can’t store in Redis.