Comments (12)
For anyone else coming here and not being sure where to start (like me), a quick example:
# settings.py
CHANNEL_LAYERS = {
# This is the channel layer id, you can have a bunch if you want
'default': {
'BACKEND': 'asgi_redis.RedisChannelLayer',
'CONFIG': {
'hosts': [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
},
# this defines the router for the layer, e.g. the definitions of channels and their handlers
'ROUTING': 'app.routing.channel_routing',
}
}
# app/routing.py
channel_routing = {
# You can name your channel anything you want, you probably see 'websocket.*' in most tutorials.
'your_channel_name': lambda x: print('Channel Triggered Event!'),
}
# ./manage.py shell
from channels import Channel, channel_layers
# default here matches your layer name (from settings.py) that can delegate to your_channel_name
c = Channel('your_channel_name', channel_layer=channel_layers['default'])
c.send({})
Adding a command is pretty simple, see here and then you can just call ./manage.py commandname
from cron
from channels.
For now, I recommend writing a management command that sends the signals onto the channels, and calling that from cron.
from channels.
Yes, the intention is to have some sort of command that can push messages onto channels periodically. In the meantime, I suggest writing a custom management command that does this and calling it using cron
.
from channels.
Thanks, sounds great
from channels.
Hello Andrew. Thank you for the great project!
Can you (or someone else) suggest the best way to send signals to channels via cron?
from channels.
Yup, that's a great example - thanks @mrasband! I'll see if I can work stuff for calling from cron into the docs - it also looks like we're going to have some work start on a scheduled task interface server soon (similar to celerybeat), which might be better if you want things very frequently or can't set cron tasks.
from channels.
Thank you @andrewgodwin for the awesome work, this has helped swing me back to using Django more :)
from channels.
I believe @jacobian was looking into it, but I don't know what sort of progress he's made, or if he's found the time to start yet.
from channels.
You can do it in Twisted in three lines:
from twisted.internet.task import LoopingCall
looper = LoopingCall(send_some_message_function)
looper.start(1)
looper.start(1)
will call the function send_some_message_function
once every second.
from channels.
looper.stop()
from channels.
I went ahead and created a thin wrapper around apscheduler add_job command that'll send a message back on a specified channel when triggered in my own asgi implementation.
It would be simple to squeeze it into Daphne too and solves most, if not all, delayed-call / scheduling issues.
from channels.
Closing this as there's delay for channels 1 and in channels 2 you can just use asyncio stuff.
from channels.
Related Issues (20)
- OriginValidator could raise exception/log message for denied connections HOT 1
- Unable to collect code coverage when using `ChannelsLiveServerTestCase` HOT 14
- error in channel layer explanations HOT 2
- adding a check if group_name exists method for channel_layer HOT 1
- Small messages are getting concatenated HOT 2
- How can I change the schema in the database? HOT 1
- How to test send_json() contents when passing close=True HOT 2
- Handler declaration by decorator/annotation (in order to prevent potential method leaks) HOT 1
- django-q2 and django-channels together HOT 1
- Channel Layer's group_send() Not Working When Called From Django View HOT 3
- Add CORs middleware HOT 1
- Breakage in routing.py on django/main pre-5.1.
- Use Django async-native APIs where possible HOT 4
- Django Channels Memory Leak on every message or connection HOT 26
- WARNING daphne.server ASGIStaticFilesHandler WebRequest took too long to shut down and was killed HOT 1
- Use group_send() when group is empty HOT 1
- Session Data Corrupted HOT 1
- Error with send_json Function in AsyncJsonWebsocketConsumer HOT 6
- Getting TypeError: SSEConsumer() missing 2 required positional arguments: 'receive' and 'send'
- django.core.exceptions.SynchronousOnlyOperation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from channels.