https://github.com/synw/djangoinstant
Client for the django-instant websockets backend
https://github.com/synw/djangoinstant
Last synced: about 2 months ago
JSON representation
Client for the django-instant websockets backend
- Host: GitHub
- URL: https://github.com/synw/djangoinstant
- Owner: synw
- License: mit
- Created: 2021-05-31T13:20:34.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-10T10:53:30.000Z (over 2 years ago)
- Last Synced: 2025-04-14T19:07:56.090Z (about 2 months ago)
- Language: TypeScript
- Size: 14.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Django Instant client
[](https://www.npmjs.com/package/djangoinstant)
Client for the [django-instant](https://github.com/synw/django-instant) websockets backend
```
npm install djangoinstant
// or
yarn add djangoinstant
```## Usage
Initialize the client
```typescript
import { useInstant } from "djangoinstant";const instant = useInstant();
await instant.init(
"http://localhost:8000", // backend url
"ws://localhost:8427", // websockets url
true // verbosity
)
````init` structure:
```typescript
(backendUrl: string, websocketsUrl: string, verbose?: boolean) => Promise
```### Get a websockets connection authorization
Login with Django and get credentials for websockets:
```typescript
await instant.login("some_username", "some_password");
```If the user is already logged in just get the websockets connection credentials:
```typescript
await instant.get_token();
```### Define handlers for messages
Define a handler function and use it for all incoming messages:
```typescript
import { Message } from "djangoinstant";function onMessage(msg: Message): void {
switch (msg.channelName) {
case "public":
// process msg
break;
case "$users":
// process msg
break;
case "$group1":
// process msg
break;
default:
throw new Error(`Unknown channel ${msg.channelName}`)
}
}instant.onMessage(onMessage);
```Message structure:
```typescript
class Message {
channelName: string;
msg: string;
data: Record | Array;
eventClass: string;
site: string;
bucket: string | null;
date: Date;
}
```### Connect to the websockets server
```typescript
await instant.connect();
console.log("Websockets connected");
```By default the `connect` function will subscribe to all the authorized channels
for the user provided by the backend. To avoid this use `await instant.connect(false)`. To
subscribe later to all channels:```typescript
instant.subscribe();
```Note: a classic [centrifuge-js](https://github.com/centrifugal/centrifuge-js) client is
accessible with `instant.getClient()`## Example
An [example](https://github.com/synw/django-instant-example) with a backend and a frontend is available