Skip to content

Commit b88ed6a

Browse files
authored
Improves chat (#173)
* Follow up * Update jupyter_ydoc
1 parent b1094c2 commit b88ed6a

File tree

9 files changed

+69
-41
lines changed

9 files changed

+69
-41
lines changed

jupyter_collaboration/handlers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import asyncio
77
import json
8+
import time
89
import uuid
910
from pathlib import Path
1011
from typing import Any
@@ -216,7 +217,6 @@ def on_message(self, message):
216217
On message receive.
217218
"""
218219
message_type = message[0]
219-
print("message type:", message_type)
220220

221221
if message_type == YMessageType.AWARENESS:
222222
# awareness
@@ -245,8 +245,12 @@ def on_message(self, message):
245245

246246
if message_type == MessageType.CHAT:
247247
msg = message[2:].decode("utf-8")
248+
248249
user = self.get_current_user()
249-
data = json.dumps({"username": user.username, "msg": msg}).encode("utf8")
250+
data = json.dumps(
251+
{"sender": user.username, "timestamp": time.time(), "content": json.loads(msg)}
252+
).encode("utf8")
253+
250254
for client in self.room.clients:
251255
if client != self:
252256
task = asyncio.create_task(

packages/collaboration-extension/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"dependencies": {
5656
"@jupyter/collaboration": "^1.0.1",
5757
"@jupyter/docprovider": "^1.0.1",
58+
"@jupyter/ydoc": "^1.1.0-a0",
5859
"@jupyterlab/application": "^4.0.0",
5960
"@jupyterlab/apputils": "^4.0.0",
6061
"@jupyterlab/codemirror": "^4.0.0",

packages/collaboration-extension/src/collaboration.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ import {
1414
EditorExtensionRegistry,
1515
IEditorExtensionRegistry
1616
} from '@jupyterlab/codemirror';
17+
import { WebSocketAwarenessProvider } from '@jupyter/docprovider';
18+
import { SidePanel, usersIcon } from '@jupyterlab/ui-components';
19+
import { URLExt } from '@jupyterlab/coreutils';
20+
import { ServerConnection } from '@jupyterlab/services';
21+
import { IStateDB, StateDB } from '@jupyterlab/statedb';
22+
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
23+
24+
import { Menu, MenuBar } from '@lumino/widgets';
25+
26+
import { IAwareness } from '@jupyter/ydoc';
27+
1728
import {
1829
CollaboratorsPanel,
1930
IGlobalAwareness,
@@ -23,13 +34,6 @@ import {
2334
UserInfoPanel,
2435
UserMenu
2536
} from '@jupyter/collaboration';
26-
import { IAwareness, WebSocketAwarenessProvider } from '@jupyter/docprovider';
27-
import { SidePanel, usersIcon } from '@jupyterlab/ui-components';
28-
import { Menu, MenuBar } from '@lumino/widgets';
29-
import { URLExt } from '@jupyterlab/coreutils';
30-
import { ServerConnection } from '@jupyterlab/services';
31-
import { IStateDB, StateDB } from '@jupyterlab/statedb';
32-
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
3337

3438
import * as Y from 'yjs';
3539
import { Awareness } from 'y-protocols/awareness';

packages/collaboration/src/tokens.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
import type { Menu } from '@lumino/widgets';
55
import { Token } from '@lumino/coreutils';
6-
import { IAwareness } from '@jupyter/docprovider';
76
import type { User } from '@jupyterlab/services';
87

8+
import { IAwareness } from '@jupyter/ydoc';
9+
910
/**
1011
* The user menu token.
1112
*

packages/docprovider/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"watch": "tsc -b --watch"
4242
},
4343
"dependencies": {
44-
"@jupyter/ydoc": "^1.0.2",
44+
"@jupyter/ydoc": "^1.1.0-a0",
4545
"@jupyterlab/coreutils": "^6.0.0",
4646
"@jupyterlab/services": "^7.0.0",
4747
"@lumino/coreutils": "^2.1.0",

packages/docprovider/src/awareness.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@
66
import { User } from '@jupyterlab/services';
77

88
import { IDisposable } from '@lumino/disposable';
9-
import { ISignal, Signal } from '@lumino/signaling';
9+
import { IStream, Stream } from '@lumino/signaling';
10+
11+
import { IAwareness } from '@jupyter/ydoc';
1012

1113
import * as decoding from 'lib0/decoding';
1214
import * as encoding from 'lib0/encoding';
1315
import { WebsocketProvider } from 'y-websocket';
1416

15-
import { IAwareness, IAwarenessProvider } from './tokens';
17+
import { IAwarenessProvider } from './tokens';
1618

1719
export enum MessageType {
1820
CHAT = 125
1921
}
2022

23+
export interface IContent {
24+
type: string;
25+
body: string;
26+
}
27+
2128
export interface IChatMessage {
22-
username: string;
23-
msg: string;
29+
sender: string;
30+
timestamp: number;
31+
content: IContent;
2432
}
2533

2634
/**
@@ -45,13 +53,13 @@ export class WebSocketAwarenessProvider
4553

4654
this._awareness = options.awareness;
4755

48-
const user = options.user;
49-
user.ready
50-
.then(() => this._onUserChanged(user))
56+
this._user = options.user;
57+
this._user.ready
58+
.then(() => this._onUserChanged(this._user))
5159
.catch(e => console.error(e));
52-
user.userChanged.connect(this._onUserChanged, this);
60+
this._user.userChanged.connect(this._onUserChanged, this);
5361

54-
this._chatMessage = new Signal(this);
62+
this._messageStream = new Stream(this);
5563

5664
this.messageHandlers[MessageType.CHAT] = (
5765
encoder,
@@ -62,8 +70,7 @@ export class WebSocketAwarenessProvider
6270
) => {
6371
const content = decoding.readVarString(decoder);
6472
const data = JSON.parse(content) as IChatMessage;
65-
console.debug('Chat:', data);
66-
this._chatMessage.emit(data);
73+
this._messageStream.emit(data);
6774
};
6875
}
6976

@@ -74,17 +81,18 @@ export class WebSocketAwarenessProvider
7481
/**
7582
* A signal to subscribe for incoming messages.
7683
*/
77-
get chatMessage(): ISignal<this, IChatMessage> {
78-
return this._chatMessage;
84+
get messageStream(): IStream<this, IChatMessage> {
85+
return this._messageStream;
7986
}
8087

8188
dispose(): void {
8289
if (this._isDisposed) {
8390
return;
8491
}
8592

86-
this.destroy();
93+
this._user.userChanged.disconnect(this._onUserChanged, this);
8794
this._isDisposed = true;
95+
this.destroy();
8896
}
8997

9098
/**
@@ -93,10 +101,13 @@ export class WebSocketAwarenessProvider
93101
* @param msg message
94102
*/
95103
sendMessage(msg: string): void {
96-
console.debug('Send message:', msg);
104+
const data: IContent = {
105+
type: 'text',
106+
body: msg
107+
};
97108
const encoder = encoding.createEncoder();
98109
encoding.writeVarUint(encoder, MessageType.CHAT);
99-
encoding.writeVarString(encoder, msg);
110+
encoding.writeVarString(encoder, JSON.stringify(data));
100111
this.ws!.send(encoding.toUint8Array(encoder));
101112
}
102113

@@ -105,9 +116,10 @@ export class WebSocketAwarenessProvider
105116
}
106117

107118
private _isDisposed = false;
119+
private _user: User.IManager;
108120
private _awareness: IAwareness;
109121

110-
private _chatMessage: Signal<this, IChatMessage>;
122+
private _messageStream: Stream<this, IChatMessage>;
111123
}
112124

113125
/**

packages/docprovider/src/tokens.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { DocumentChange, YDocument } from '@jupyter/ydoc';
55
import { Contents } from '@jupyterlab/services';
66

77
import { Token } from '@lumino/coreutils';
8-
import { ISignal } from '@lumino/signaling';
9-
10-
import type { Awareness } from 'y-protocols/awareness';
8+
import { IStream } from '@lumino/signaling';
119

1210
import { IChatMessage } from './awareness';
1311

@@ -52,21 +50,14 @@ export interface ISharedModelFactory extends Contents.ISharedFactory {
5250
): void;
5351
}
5452

55-
/**
56-
* The awareness interface.
57-
*
58-
* TODO: Move to @jupyter/YDoc
59-
*/
60-
export type IAwareness = Awareness;
61-
6253
/**
6354
* A provider interface for global awareness features.
6455
*/
6556
export interface IAwarenessProvider {
6657
/**
6758
* A signal to subscribe for incoming messages.
6859
*/
69-
get chatMessage(): ISignal<this, IChatMessage>;
60+
readonly messageStream: IStream<this, IChatMessage>;
7061

7162
/**
7263
* Send a message to every collaborator.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ classifiers = [
2828
]
2929
dependencies = [
3030
"jupyter_server>=2.0.0,<3.0.0",
31-
"jupyter_ydoc>=1.0.1,<2.0.0",
31+
"jupyter_ydoc>=1.1.0a0,<2.0.0",
3232
"ypy-websocket>=0.12.1,<0.13.0",
3333
"jupyter_events",
3434
"jupyter_server_fileid>=0.6.0,<1"

yarn.lock

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,7 @@ __metadata:
20212021
dependencies:
20222022
"@jupyter/collaboration": ^1.0.1
20232023
"@jupyter/docprovider": ^1.0.1
2024+
"@jupyter/ydoc": ^1.1.0-a0
20242025
"@jupyterlab/application": ^4.0.0
20252026
"@jupyterlab/apputils": ^4.0.0
20262027
"@jupyterlab/builder": ^4.0.0
@@ -2075,7 +2076,7 @@ __metadata:
20752076
version: 0.0.0-use.local
20762077
resolution: "@jupyter/docprovider@workspace:packages/docprovider"
20772078
dependencies:
2078-
"@jupyter/ydoc": ^1.0.2
2079+
"@jupyter/ydoc": ^1.1.0-a0
20792080
"@jupyterlab/coreutils": ^6.0.0
20802081
"@jupyterlab/services": ^7.0.0
20812082
"@jupyterlab/testing": ^4.0.0
@@ -2129,6 +2130,20 @@ __metadata:
21292130
languageName: node
21302131
linkType: hard
21312132

2133+
"@jupyter/ydoc@npm:^1.1.0-a0":
2134+
version: 1.1.0-a0
2135+
resolution: "@jupyter/ydoc@npm:1.1.0-a0"
2136+
dependencies:
2137+
"@jupyterlab/nbformat": ^3.0.0 || ^4.0.0-alpha.21 || ^4.0.0
2138+
"@lumino/coreutils": ^1.11.0 || ^2.0.0
2139+
"@lumino/disposable": ^1.10.0 || ^2.0.0
2140+
"@lumino/signaling": ^1.10.0 || ^2.0.0
2141+
y-protocols: ^1.0.5
2142+
yjs: ^13.5.40
2143+
checksum: 0099bacb2884a460867658e7f5a944e4d6eca7c4d3d68a6b31102be77d8fc9b6ba162af55c20f81c70f7fef4e9d9329eee4da32063c0caa0c6e3f10a488f95b5
2144+
languageName: node
2145+
linkType: hard
2146+
21322147
"@jupyterlab/application@npm:^4.0.0":
21332148
version: 4.0.0
21342149
resolution: "@jupyterlab/application@npm:4.0.0"

0 commit comments

Comments
 (0)