Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rocket Chat Realtime API in browser

I want to create a Rocket Chat client to subscribe to a channel through browser using Realtime API. Documentation here does not provide step by step procedure. Please let me know how to achieve it.

Links to any documentation would be very helpful.

like image 992
Nandan Acharya Avatar asked Jun 11 '18 08:06

Nandan Acharya


People also ask

Does Rocket chat use Websockets?

Our real-time API is composed of two elements: Method Calls and Subscriptions. Both of them are supported directly in the websocket connection.

How do I access RocketChat?

To connect to a Rocket. Chat server through a web browser, enter the desired server address in the browser's address bar, such as https://open.rocket.chat . After entering a valid Rocket. Chat server address, the registration page appears.

Is Rocket chat self hosted?

FAQs about Rocket.Yes, you can self-host it and use your Community Edition workspace for free, forever.

Does Rocket chat have an app?

The Rocket. Chat mobile app is developed with React Native, helping us create both Android and iOS applications from a single codebase.


1 Answers

I had less idea about websockets when I asked this question. For the benefit of all, mentioning the steps I followed.

  1. Open websocket
    var rocketChatSocket = new WebSocket("ws://locahost:3000/websocket");
  1. Connect
    var connectRequest = {
        "msg": "connect",
        "version": "1",
        "support": ["1", "pre2", "pre1"]
     }
     rocketChatSocket.send(JSON.stringify(connectRequest));

After connecting, keep responding with {"msg":"pong"} for {"msg":"ping"} from server.

  1. Login with authToken received by calling this API
     var loginRequest = {
        "msg": "method",
        "method": "login",
        "id": "42",
        "params": [
            { "resume": "authToken" }
          ]
    }

    rocketChatSocket.send(JSON.stringify(loginRequest));
  1. Subscribe to room
     var subscribeRequest = {
       "msg": "sub",
       "id": "unique-id",
       "name": "stream-notify-room",
       "params":[
        "room-id/event",
         false
       ]
    }

    rocketChatSocket.send(JSON.stringify(subscribeRequest));
  1. Send message
   var request={
       "msg": "method",
       "method": "sendMessage",
       "id": "42",
       "params": 
       [
          {
            "_id": "message-id",
            "rid": "room-id",
            "msg": "Hello World!"
          }
       ]
    }

    rocketChatSocket.send(JSON.stringify(request));
like image 152
Nandan Acharya Avatar answered Sep 18 '22 06:09

Nandan Acharya