[erlang-questions] State in Cowboy Websockets

Loïc Hoguin essen@REDACTED
Mon Oct 1 07:48:01 CEST 2018


Without seeing your code I have no idea why you think you can't use a 
record for the state.

Just do:

init(Req, _) ->
     {cowboy_websocket, Req, #state{}}.

On 10/01/2018 06:52 AM, Noah Silverman wrote:
> Hello,
> I've written a fairly standard websocket handler using Cowboy 2.4.  It 
> correctly accepts websockts, and handles messages in both directions.  
> Cowboy's built in functionality makes this simple.
> However, I can't find a way to store any state for a websocket 
> connection.  Cowboy creates a separate PID for each connection, so 
> theoretically we should be able to have each PID keep state (like any 
> other erlang process or gen_server.) I'd like users to be able to do 
> things like authenticate, set preferences, etc. for a websocket 
> connection.  the easiest way to handle this would be to simply save a 
> few variable sin state, as we do in any other gen_server process.
> The Cowboy Websocket handler *does* have a state variable that gets 
> passed through calls, HOWEVER there is no way to define the state record 
> like we'd do with other gen_server processes.  I tried simply adding the 
> record definition to the top of the handler file (standard workflow for 
> gen_server), but Cowboy then crashes with an undefined record error when 
> I try to use the state record.
> Does anybody have any ideas on haw to handle this, or a potential solutions?
> Thank You!
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

Loïc Hoguin

More information about the erlang-questions mailing list