开发者

Erlang/Mochiweb newbie question abt clients communication

开发者 https://www.devze.com 2023-03-21 17:25 出处:网络
Everytime the client/browser connects to Mochiweb server, it creates new process of Loop, doesn\'t it? So, if I want

Everytime the client/browser connects to Mochiweb server, it creates new process of Loop, doesn't it? So, if I want to transfer a message from one client to another (typical chat system) I should use the self() of Loop to store all connected clients PIDs, shouldn't I?

If something(or everything) is wrong so far plz explain me briefly how th开发者_如何学编程e system works, where is server process and where is client process?

How to send a message to the Loop process of client using its PID? I mean where to put the "receive" in the Loop?


Here's a good article about a Mochiweb Web Chat implemention. HTTP Clients don't have PID's as HTTP is a stateless protocol. You can use cookies to connect a request to a unique visitor of the chat room.


First, do your research right. Check out this article , and this one and then this last one.

Let the mochiweb processes bring chat data into your other application server (could be a gen_server, a worker in your OTP app with many supervisors, other distributed workers e.t.c). You should not depend on the PID of the mochiweb process. Have another way of uniquely identifying your users. Cookies, Session ids, Auth tokens e.t.c. Something managed only by your application. Let the mochiweb processes just deliver chat data to your servers as soon as its available. You could do some kind of queuing in mnesia where every user has a message queue into which other users post chat messages. Then the mochiweb processes just keep asking mnesia if there is a message available for the user at each connection. In summary, it will depend on the chat methodology: HTTP Long polling/COMET, REST/ Server push/Keep-alive connections blur blur blur....Just keep it fault tolerant and do not involve mochiweb processes in the chat engine, just let mochiweb be only transport and do your chat jungle behind it!


You can use several data structures to avoid using PIDs for identity. Take an example of a queue(). Imagine you have a replicated mnesia Database with RAM Table in which you have implemented a clearly uniquely identifiable queue() per user. A process (mochiweb process), holding a connection to the user only holds an identity of this users session. It then uses this identity to keep checking into his queue() in Mnesia at regular intervals (if you are intending to it this way -- keeping mochiweb processes alive as long as the users session). Then it means that no matter which Process PID a user is connected through, as long as the process has the users identity, then it can fetch (read) messages from his message queue(). This would consequently result in having the possibility of a user having multiple client sessions. The same process can use this identity to dump messages from this user into other users' queues().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号