Giter Site home page Giter Site logo

django-realtime's Introduction

Django-Realtime: an iShout.js client for Django.

iShout.js is a node.js + socket.io server used for adding real-time notifications to your existing web application. This project adds easy iShout.js integration for Django, thus enabling you to easily add real-time notifications to you exiting Django applications.

Before installing and using the client, make sure you setup iShout.js using this guide.

Installation

  • install the client either by cloning this repo, or, if using pip, by using the following command:

    pip install django-realtime

  • add drealtime to INSTALLED_APPS in your settings.py file.

Configuration

  • add 'drealtime.middleware.iShoutCookieMiddleware' to your MIDDLEWARE_CLASSESsettings. right above SessionMiddleware. this will take care of setting the authentication cookie.
  • in your templates, add {% load drealtimetags %} at the top of the template. inside your <head> tag, add {% ishout_js %}. this will include the proper <script> tags for you. place this tag above any js files that use the ishout.js client.
  • the following optional settings are availabe:
    • ISHOUT_CLIENT_ADDR - defaults to 'localhost:5500'. should probably be changed for production.
    • ISHOUT_API_ADDR - defaults to 'localhost:6600'.
    • ISHOUT_HTTPS - defaults to False. whether or not iShout.js is setup to use SSL.

Usage

Now, from a view, emit a message to a user:

from drealtime import iShoutClient
ishout_client = iShoutClient()

# This is our pseudo view code
def comment_on_blog(request):
    post = Post.objects.get(...)
    # Process stuff, handle forms, do whatever you want.
    ishout_client.emit(
        post.author.pk,
        'notifications',
        data={ 'msg' : 'You have a new comment!' }
    )
    # Some other things happening here..
    return render_to_response('blog_post.html', ctx)

In your HTML code, add the following JS snippet, to receive this notification on the client's side:

// use .on() to add a listener. you can add as many listeners as you want.
ishout.on('notifications', function(data){
    alert(data.msg);
});

// .init() will authenticate against the ishout.js server, and open the WebSocket connection.
ishout.init();

Your client should receive an alert with the notification. the channel name (notifications) and the data you send are completley up to you, so be creative and use it wisely.

If you have more than one channel we want to listen on, we can also chain these calls:

ishout.on('notifications', function(data){
    popupNotification(data.msg);

}).on('alerts', function(data)}{
    alert(data.alert_msg);

}).on('something-else', function(data){
    $('#someDiv').html(data.htmlContent);
    
}).init();

License

(The MIT License)

Copyright (c) 2012 Anish menon [email protected];

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

django-realtime's People

Contributors

alecxe avatar burhan avatar valignatev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

django-realtime's Issues

Django 1.9 issue

Hi,

{% ishout_setup %} return '<' instead of '<' and so on.
To fix that, you have to mark your return string as safe.
from django.utils.safestring import mark_safe
return mark_safe(<script> .....)

Regards,
Robin

Getting error when trying to connect to Redis server

Hi!

I have done everything exactly like you presented in the tutorial video.

But when I try to run node server.js, it seems that it fails to connect to the Redis server for some reason. The redis server is up and running on port 6379, I've checked it with redis-cli set/get commands.

My error message:

storage backend: Redis
Started API server on host: localhost, port: 6600
Started socket.io API on host: 0.0.0.0, port: 5500
Started pub/sub listener

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED
    at RedisClient.on_error (/usr/local/lib/node_modules/ishout.js/node_modules/redis/index.js:196:24)
    at Socket.<anonymous> (/usr/local/lib/node_modules/ishout.js/node_modules/redis/index.js:106:14)
    at Socket.emit (events.js:67:17)
    at Array.1 (net.js:319:25)
    at EventEmitter._tickCallback (node.js:192:41)

Generate new token cookie when current token is invalid

When a token cookie is already set and you restart Redis, future authentications fail. It might be a good idea to get the token for the current user and to compare it with the token cookie before setting or not the cookie.

get online users

Hi, I'm writing a simple web-chat using drealtime. Now here's the issue. I need to update user list on a client side, when someone logs in or out. I can simply get bunch of users using iShoutClient.get_room_status(), but I don't know how to trigger the function when someone joins or leaves a room. The obvious way I can do it is by running a cron or celery job, checking users online for every 5 seconds and refreshing info on a client side if a change has happened. But I think I could use drealtime or socketio broke or establishing the connection event. Could please you give me a follow. Merry Christmas and a Happy new Year!

Python3.3 compatibility

hi brow , im workin on Python3 an find when using this package import simplejson as json raised thi error from django.utils import simplejson as json
ImportError: cannot import name 'simplejson' and then update from this repository but still there is error on this line , after i search about it, i find python3.3 has "json" package instead of "simplejson" and when import simplejson as json changed into import json it works as well as ...
thanks and goodluck .

ssl connection

ssl connection someone had problem when running the server node node_modules / ishout.js / serverjs

storage backend: Redis
Started API server on host: localhost, port: 6600
Running socket.IO in SSL mode
Started socket.io API on host: 0.0.0.0, port: 5500
Started pub/sub listener
node: ../src/util-inl.h:196: TypeName* node::Unwrap(v8::Localv8::Object) [with TypeName = node::TLSWrap]: Assertion `(object->InternalFieldCount()) > (0)' failed.
Aborted (core dumped)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.