|
22 | 22 | * [set_default_scope](#set_default_scope) |
23 | 23 | * [wait_closed](#wait_closed) |
24 | 24 | * [Room](#room) |
25 | | - |
| 25 | + * [methods](#room-methods) |
| 26 | + * [properties](#room-properties) |
| 27 | + * [join](#join) |
| 28 | + * [leave](#leave) |
| 29 | + * [emit](#emit) |
26 | 30 | --------------------------------------- |
27 | 31 |
|
28 | 32 | ## Installation |
@@ -421,4 +425,119 @@ connection is actually closed. |
421 | 425 |
|
422 | 426 | ## Room |
423 | 427 |
|
| 428 | +Rooms can be implemented to listen for events from ThingsDB rooms. |
| 429 | + |
| 430 | +Se the example code: |
| 431 | + |
| 432 | +```python |
| 433 | +from thingsdb.room import Room, event |
| 434 | + |
| 435 | +class Chat(Room): |
| 436 | + |
| 437 | + @event('msg') |
| 438 | + def on_msg(self, msg): |
| 439 | + print(msg) |
| 440 | + |
| 441 | +``` |
| 442 | + |
| 443 | +This will listen for `msg` events on a ThingsDB room. To connect out class to a |
| 444 | +room, you have to initialize the class with a `roomId` of some ThingsDB code which |
| 445 | +returns the `roomId` as integer value. For example: |
| 446 | + |
| 447 | +```python |
| 448 | +# Create a chat instance. In this example we initialize our chat with some ThingsDB code |
| 449 | +chat = Chat("""//ti |
| 450 | + // Create .chat room if the room does not exist. |
| 451 | + try(.chat = room()); |
| 452 | +
|
| 453 | + // return the roomId. |
| 454 | + .chat.id(); |
| 455 | +""") |
| 456 | + |
| 457 | +# Now we can join the room. (we assume that you have a ThingsDB client) |
| 458 | +await chat.join(client) |
| 459 | +``` |
| 460 | + |
| 461 | +## Room Methods |
| 462 | + |
| 463 | +Besides implementing an `@event` handler, a room has also some methods which can be implemented to control or initialize a room. |
| 464 | + |
| 465 | +### on_init(self) -> None |
| 466 | +Called when a room is joined. This method will be called only once, |
| 467 | +thus *not* after a re-connect like the `on_join(..)` method. This |
| 468 | +method is guaranteed to be called *before* the `on_join(..)` method. |
| 469 | + |
| 470 | +### on_join(self) -> None: |
| 471 | +Called when a room is joined. Unlike the `on_init(..)` method, |
| 472 | +the `on_join(..)` method will be called again after a re-connect. |
| 473 | + |
| 474 | +This is an async method and usually the best method to perform |
| 475 | +some ThingsDB queries (if required). |
| 476 | + |
| 477 | +Unless the `wait` argument to the Room.join(..) function is explicitly |
| 478 | +set to None, the first call to this method will finish before the |
| 479 | +call the Room.join() is returned. |
| 480 | + |
| 481 | +### on_leave(self) -> None: |
| 482 | +Called after a leave room request. This event is *not* triggered |
| 483 | +by ThingsDB when a client disconnects or when a node is shutting down. |
| 484 | + |
| 485 | +### on_delete(self) -> None: |
| 486 | +Called when the room is removed from ThingsDB. |
| 487 | + |
| 488 | +## Room Properties |
| 489 | + |
| 490 | +The following properties are available on a room instance. Note that some properties |
| 491 | +might return `None` as long as a room is not joined. |
| 492 | + |
| 493 | +Property | Description |
| 494 | +-------- | ----------- |
| 495 | +`id` | Returns the roomId. |
| 496 | +`scope` | Returns the scope of the room. |
| 497 | +`client` | Returns the associated client of the room. |
| 498 | + |
| 499 | +### join |
| 500 | + |
| 501 | +```python |
| 502 | +Room().join(client: Client, wait: Optional[float] = 60) -> None |
| 503 | +``` |
| 504 | + |
| 505 | +Joins the room. |
| 506 | + |
| 507 | +#### Args |
| 508 | +- client *(thingsdb.client.Client)*: |
| 509 | + ThingsDB client instance. |
| 510 | +- wait *(float)*: |
| 511 | + Max time (in seconds) to wait for the first `on_join` call. |
| 512 | + If wait is set to None, the join method will not wait for |
| 513 | + the first `on_join` call to happen. |
| 514 | + |
| 515 | + |
| 516 | +### leave |
| 517 | + |
| 518 | +```python |
| 519 | +Room().leave() -> None |
| 520 | +``` |
| 521 | + |
| 522 | +Leave the room. If the room is not found, a `LookupError` will be raised. |
| 523 | + |
| 524 | +### emit |
| 525 | + |
| 526 | +```python |
| 527 | +Room().emit(event: str, *args: Optional[Any],) -> asyncio.Future |
| 528 | +``` |
| 529 | + |
| 530 | +Emit an event to a room. |
| 531 | + |
| 532 | +#### Args |
| 533 | + |
| 534 | +- *event (str)*: |
| 535 | + Name of the event to emit. |
| 536 | +- *\*args (any)*: |
| 537 | + Additional argument to send with the event. |
| 538 | + |
| 539 | +#### Returns |
| 540 | + |
| 541 | +Future which should be awaited. The result of the future will |
| 542 | +be set to `None` when successful. |
424 | 543 |
|
0 commit comments