Skip to main content

Notification/Alert for django users

Project description

Simple Notification

A basic & minimal example, of handling notification using websocket.

Tools

Installation

On your terminal/shell

pip3 install ibalert

sudo docker run -p 6379:6379 -d redis:5

Quiz setup


In your project's settings.py file. Add ibalert and channels to intallled apps and configure redis.

# settings.py

INSTALLED_APPS = [
  ...,
  "ibalert",
  "channels"
]

ASGI_APPLICATION = "ibalert.asgi.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {"hosts": [("localhost", 6379)]},
    }
}

In your project's urls.py

...
from django.views.generic import TemplateView

urlpatterns = [
    path("", TemplateView.as_view(template_name="home.html")),
    ...
]

In your templates directory add home.html file. (Just to make sure the app is working. Later you can implement it the way you want.)

<html>
  <head>
    <title>IB-Alert</title>
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
    />
  </head>

  <body>
    <div class="container">
      <h4 class="text-center">
        Notifications
        <span class="badge bg-danger" id="counter">0</span>
      </h4>

      <ul id="notifylist"></ul>
    </div>
  </body>
  <script>
    const webSocket = new WebSocket("ws://localhost:8000/notifications/")
    webSocket.onclose = function (e) {
      console.error("Chat socket closed unexpectedly")
    }
    webSocket.onopen = function (e) {
      webSocket.send(JSON.stringify({ userID: 1 }))
    }
    webSocket.onmessage = function (action) {
      const data = JSON.parse(action.data)
      console.log(data.event == "Notification", data)
      const nl = document.querySelector("#notifylist")
      if (data.event == "Notification") {
        var counter = document.getElementById("counter")
        counter.innerText = data.unread_count
        var el = document.createElement("li")
        el.innerHTML = `<b>New Notification </b>: ${data.text}!`
        nl.appendChild(el)
      }
    }
  </script>
</html>

Now make migrations, migrate, createsuperuser and runserver. You should see something like below.

starting ASGI/Channels version 3.0.4 development server at http://127.0.0.1:8000/

Open django shell python3 manage.py shell along with open http://127.0.0.1:8000 on a browser. And run the following on your django shell.

from ibalert.models import Notifications
Notificatons.objects.create(text='hello there!', user_id=1)

You should see new notification on your browser.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibalert-0.1.2.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file ibalert-0.1.2.tar.gz.

File metadata

  • Download URL: ibalert-0.1.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for ibalert-0.1.2.tar.gz
Algorithm Hash digest
SHA256 42b26f2cbb2bd42747c4f2741e84f51c4a9449c53bf2e60303cb8c019d6570db
MD5 3f1221eaf065cd953caad088f48a031d
BLAKE2b-256 3e2fa4e43f878c13baf105f94f89e2c48d0997a628a07aed986e78599b938e6a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page