How to listen to real time event zulip using js and ajax

211 Views Asked by At

I have integrated zulip api using js and ajax , the problem is that the exchanged messages are not in real tile so pleasen, how to listen to real time event zulip using js and ajax, here is my js code

$("#post").click(function () {

var url = 'https://domain_name/api/v1/messages';
var content = $("#postButton").val();
$.ajax({
    type: "post",
    url: url,
    data: {type: 'private', content: content, to: '[email protected]'},
    headers: {Authorization: getBasicAuthenticationToken(username, 
    password)},
   

   })






  });


$(function () {
var $msgs = $('#msgs');


 $.ajax({
    type: 'GET',
    url: 'https://domain_name/api/v1/messages',
    data: {num_before: 10, num_after: 1000, use_first_unread_anchor: true},
    headers: {
        "Authorization": getBasicAuthenticationToken(username, password)
    },
    success: function (msgs) {
   
        /*console.log(msgs['messages'][0]['sender_email']);*/
        $.each(msgs.messages, function (i, msg) {
            $msgs.append('<li>' + msg.content + '</li>');
            $msgs.append('<li>' + msg.sender_email + '</li>');
            var who = msg.sender_email;

            insertChat('<p>' + who + '</p>', '<p>' + msg.content +'</p>',0)
        });
    }
});
});
1

There are 1 best solutions below

0
Anders Kaseorg On

The API you need is the Zulip real-time events API. First, register an event queue with GET /api/v1/register, specifying event_types=["message"]. Then you can get events from the queue by repeatedly long-polling GET /api/v1/events.