Question

Is there any way to get a "handle" (e.g. a Pid) to a client when using the handle_message/1 callback? so I can send messages to specific clients, and spawn processes with a list of clients as argument (e.g. for a game/chat session) for a specific group of clients.

Or is there any other way I should solve this with Yaws? I haven't found it in any documentation or example on the Yaws website.

Was it helpful?

Solution

In your out/1 function where you return {websocket, CallbackModule, Options} to upgrade your connection to WebSocket, you can set the Options value to indicate that your callback module is an advanced callback module, which lets you also specify an initial state value that's handed to your callback module each time Yaws calls it. If there's client-specific information sent in the initial HTTP request, your out/1 function will receive that info in its #arg{} argument, so you can retrieve it from there and return it as part of your initial state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top