hagsteel / swampdragon

swampdragon
Other
557 stars 74 forks source link

User specific channel #186

Open akhilkochuveettilanilkumar opened 8 years ago

akhilkochuveettilanilkumar commented 8 years ago

Hi, I'm woking on a project (runs on django 1.8 ) in which I require user specific push notification. I have successfully created a channel and broadcast data to all user using publish_data(). At present we have 10 million users. So I need to send a user specific push notification. I have used the following example to setup the swampdragon user specific notification: https://github.com/CptLemming/swampdragon_notification_example Using the above example I tried setting up swampdragon in my project. When I execute my project I'm not getting any notification or error regarding it.

My models.py:

    from swampdragon.models import SelfPublishModel
    from swampdragon_serializer import NotificationSerializer

   class Mymodel(SelfPublishModel, BaseModel):        
        serializer_class = NotificationSerializer
        message = models.TextField()
        user = models.ForeignKey(User)

My swampdragon_serializer.py (I'm using this file because models.py is already imported in the serializers.py so I won't be able to import serializers.py to my models.py for swamp dragon)

    from swampdragon.serializers.model_serializer import ModelSerializer

    class NotificationSerializer(ModelSerializer):
        class Meta:
            model = 'app.Mymodel'
            publish_fields = ('message', )
            update_fields = ('message', )

My router.py and notification.js looks the same as the example in the url mentioned above. When I execute my project I'm not getting any notification or error. I'm getting values from http://localhost:9999/data/info My app is also not able to subscribe the channel. Since its not showing the console.log() (mentioned in the subscribe() ) in the browser console.

AlexejStukov commented 8 years ago

Hi @Wineartist , paste the following code inside your settings.py to enable error logging:

if DEBUG:
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'simple': {
                'format': '%(levelname)s %(message)s'
            },
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
        },
        'loggers': {
            'tornado.general': {
                'handlers': ['console'],
                'level': 'ERROR',
                'propagate': True,
            }
        }
    }
ZuSe commented 8 years ago

@Wineartist

Do you have swampdragon auth installed and HttpDataConnection set?

We also use user specific push notifications in our projects. Here is an example for a router that pushes generic notifications to users defined as targets in the proper model

class GenericNotificationsRouter(ModelRouter):
    valid_verbs = ['subscribe']
    route_name = 'generic-notifications'
    model = GenericNotification
    serializer_class = GenericNotificationSDSerializer

    @login_required
    def subscribe(self, **kwargs):
        super().subscribe(**kwargs)

    def get_subscription_contexts(self, **kwargs):
        return {'receivers__id__exact': self.connection.user.pk}

route_handler.register(GenericNotificationsRouter)

Hope that helps you to understand the basic principle.

denizs commented 8 years ago

@ZuSe

I assume that your GenericNotification has a field called receivers, right? Does this field contain a list of users or solely one? I'm currently working on a private messaging app, trying to figure out how to restrict the broadcast of new messages to only the author/recepient.

ZuSe commented 8 years ago

This field does contain a list of users. But you can use the same approach for only one user. ''' return {'receiver_id': self.connection.user.pk} '''

denizs commented 8 years ago

@ZuSe Thanks for the info. :+1:

metazet commented 8 years ago

Hi all, I'm trying to make pushing messages to separated channels and can't understand why swampdragon send all messages via one socket. Here is my simple route:

from swampdragon.route_handler import BaseRouter
class OliveRouter(BaseRouter):
    route_name = 'olive-integration'

    def get_subscription_channels(self, **kwargs):
        return ['olive-GB104']
route_handler.register(OliveRouter)

I subscribe on UI to channel 'olive-GB699', then send message to channel 'olive-GB104' and see this message in both channels! Look at screenshot:

screenshot 2016-01-27 18 53 50

May be I do something wrong? I send message like that:

from swampdragon.pubsub_providers.data_publisher import publish_data
publish_data(channel='olive-{code}'.format(code=receiver_code), data={...})