1

It seems with Django Channels each time anything happens on the websocket there is no persistent state. Even within the same websocket connection, you can not preserve anything between each call to receive() on a class based consumer. If it can't be serialized into the channel_session, it can't be stored.

I assumed that the class based consumer would be persisted for the duration of the web socket connection.

What I'm trying to build is a simple terminal emulator, where a shell session would be created when the websocket connects. Read data would be passed as input to the shell and the shell's output would be passed out the websocket.

I can not find a way to persist anything between calls to receive(). It seems like they took all the bad things about HTTP and brought them over to websockets. With each call to conenct(), recieve(), and disconnect() the whole Consumer class is reinstantiated.

So am I missing something obvious. Can I make another thread and have it read from a Group?

Edit: The answers to this can be found in the comments below. You can hack around it. Channels 3.0 will not instantiate the Consumers on every receive call.

kagronick
  • 2,552
  • 1
  • 24
  • 29
  • that's because the consumers are not meant to do anything besides processing websocket requests. If you want to persist data, you have the database or django cache. Or any third-party solution you want. – hoefling Sep 16 '17 at 09:55
  • I dont want to persist anything. I want to use objects more complicated than numbers, strings, lists, and dicts for the duration of the web socket. How could someone make anything that needs sockets with the current setup? I found that I was able to make it using the low level Redis API and sending messages from the recieve calls through the pub/sub Redis layer to a thread I started in the connect method. Seems like a terrible hack to do something this simple. – kagronick Sep 16 '17 at 18:32
  • Could [this](http://channels.readthedocs.io/en/stable/getting-started.html#persisting-data) be useful? Never tried it myself, though. – hoefling Sep 18 '17 at 19:53
  • @hoefling No this is the while issue. You can only persist data that can be serialized. Strings, lists, dicts etc... You can serialize sockets and other complex objects. I talked to a Channels developer and he said that Channels 3.0 will not instantiate the Consumer on every action. Daphne and the Workers won't be separated on the network layer either. – kagronick Sep 20 '17 at 16:27
  • you mean channels 2.0? Yeah, it should bring a lot of improvements; waiting for that myself. – hoefling Sep 20 '17 at 16:41
  • Yeah 2.0. Said its not coming out for months or years. Ive made a few more consumers since then. Its a huge limitation. You can start a stream that runs indefinitly but it requires hacky fixes to stop it. – kagronick Sep 30 '17 at 15:20

1 Answers1

0

The new version of Channels does not have this limitation. Consumers stay in memory for the duration of the websocket request.

kagronick
  • 2,552
  • 1
  • 24
  • 29