---
# System prepended metadata

title: Websocket Command API.

---

# Websocket Command API. 

## Send Text Message
Send a message event to a room. 

Implement: `PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}` [Ref](https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid)

Flow : 
1. Client send a websocket request. 
2. Websocket call Matrix API to store to the DB. 
3. Websocket gets all related users. 
4. If users connected to the Websocket, send sync/stream for all related users. 

**Websocket request**

`websockets.sendJON()`

Client request: 
```json
{
    "module": "room",
    "method": "send_text",
    "params": {
        "room_id": "!d41d8cd:lalepass.lale.im",
        "event_type": "m.room.message",
        "content": {
            "msgtype": "m.text",
            "body": "hello"
        }
    }
}
```

Server response:
Simillar to `sync` response. 
```json
{
    "ws_type": "update",
    "rooms" : {
        "join" : {
            "!d41d8cd:lalepass.lale.im" : {
                "timeline" :{
                    "events" : [
                        {
                            "origin_server_ts": 1629783173336,
                            "sender": "@li3845742:lalepass.lale.im",
                            "content": {
                                "body": "hi",
                                "msgtype": "m.text"
                            },
                            "type": "m.room.message"
                        }
                    ]
                }
            }
        }
    }
}
```
## Join a Room 
Implement join a room in Websockets. Reference in matrix : 
https://matrix.org/docs/spec/r0.0.1/client_server#post-matrix-client-r0-rooms-roomid-join

Steps: 
1. client send payload to join a room 
2. websocket send request to join a room 
3. websocket update the room membership in Redis. 
4. notify all the room members, a new member has joined the room 

`websockets.sendJON()`

Client request: 
```json
{
    "module": "room",
    "method": "join_room",
    "params": {
        "room_id": "!d41d8cd:lalepass.lale.im"
    }
}
```

Notify Response 
```json
{
    "ws_type": "status",
    "rooms": {
        "join": {
            "!aDLKTvuyaxrpHTkQQs:laledev6.flowring.com": {
                "timeline": {
                    "events": [
                        {
                            "origin_server_ts": 1634720701141,
                            "sender": "@jefftest:laledev6.flowring.com",
                            "event_id": "$16347207011342tUMbb:laledev6.flowring.com",
                            "unsigned": {
                                "replaces_state": "$16347206261340zmglJ:laledev6.flowring.com",
                                "age_ts": 1634720701141
                            },
                            "state_key": "@jefftest:laledev6.flowring.com",
                            "content": {
                                "membership": "join",
                                "avatar_url": "mxc://laledev6.flowring.com/bOprZdbhJwpsaagnvIFeImbV",
                                "displayname": "Jeff test Laledev6"
                            },
                            "membership": "join",
                            "type": "m.room.member"
                        }
                    ]
                }
            }
        }
    }
}
```

## Leave a room

Flow : 
1. Client send payload to websocket 
2. send request to message broker 
3. notify all room member 
4. update redis

Client request 
```json
{
    "module": "room",
    "method": "leave_room",
    "params": {
        "room_id": "!d41d8cd:lalepass.lale.im"
    }
}
```

Websocket Response
```json 
{
    "ws_type": "status",
    "rooms": {
        "join": {
            "!aDLKTvuyaxrpHTkQQs:laledev6.flowring.com": {
                "timeline": {
                    "events": [
                        {
                            "origin_server_ts": 1634798552128,
                            "sender": "@jantest2:laledev6.flowring.com",
                            "event_id": "$16347985521407VUcfS:laledev6.flowring.com",
                            "unsigned": {
                                "replaces_state": "$16347985381406mlSqr:laledev6.flowring.com",
                                "age_ts": 1634798552128
                            },
                            "state_key": "@jantest2:laledev6.flowring.com",
                            "content": {
                                "membership": "leave"
                            },
                            "membership": "leave",
                            "type": "m.room.member"
                        }
                    ]
                }
            }
        }
    }
}
```
