Skip to main content

Support for Django's request/response lifecycle to automatically handle WebSocket messages.

Project description

# django-websocket-request

[![Build Status](]( [![PyPI version](](

## Overview
This package provides support for transport agnostic routing to allow the Django's request/response lifecycle to automatically WebSocket messages. It borrows a WebSocket message format from [Sails.js](

## Installation

Install using `pip`...

$ pip install django-websocket-request

## Usage


# Set Django Environment
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'

from wsrequest import WebSocketRequest

message = '{"method": "GET", "url": "/api/projects/"}'
request = WebSocketRequest(message)
response = request.get_response()



### WebSocket message format

The method key can be any HTTP method: GET, POST, PUT, DELETE, PATCH, HEAD, or OPTIONS. The url key is an absolute URL without the domain name. The data key is an optional dictionary in which the key-value pairs in used to create the method’s data payload. The token key is also optional, and used to recreate an HTTP Authorization header, Authorization: JWT YOUR_TOKEN_HERE. If you're using Django REST framework, you might like to check out [django-rest-framework-jwt](

"method": "POST",
"url": "/api/companies/",
"data": {
"name": "Acme Inc."
"token": "MY_JSON_WEB_TOKEN"

## Example implementation with sockjs + tornado
import os
import json

from tornado import web, ioloop
from sockjs.tornado import SockJSRouter, SockJSConnection

# Set Django Environment
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'

from blimp.utils.websockets import WebSocketRequest

class RESTAPIConnection(SockJSConnection):
def on_open(self, info):
self.send_json(json.dumps({'connected': True}))

def on_message(self, data):
response = WebSocketRequest(data).get_response()

if __name__ == '__main__':
import logging

port = 8080


Router = SockJSRouter(RESTAPIConnection, '/ws/api')

app = web.Application(Router.urls)
app.listen(port)" [*] Listening on{}".format(port))



Project details

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django_websocket_request-0.1.0-py2.py3-none-any.whl (10.2 kB) Copy SHA256 hash SHA256 Wheel 2.7
django-websocket-request-0.1.0.tar.gz (9.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page