[erlang-questions] State in Cowboy Websockets

Noah Silverman noah@REDACTED
Mon Oct 1 06:52:03 CEST 2018


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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20181001/aad33e5b/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: publickey - noah@REDACTED - 0xBCEFA873.asc
Type: application/pgp-keys
Size: 3136 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20181001/aad33e5b/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 855 bytes
Desc: OpenPGP digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20181001/aad33e5b/attachment-0001.bin>


More information about the erlang-questions mailing list