Kendo Grid using Signal R

1 Answer 49 Views
Grid
Mark
Top achievements
Rank 1
Mark asked on 12 Jan 2024, 08:23 PM | edited on 12 Jan 2024, 08:29 PM

 im attempting to have a grid update based on records in a DB

i have a log4net service sending messages to a DB

i keep getting an error 

Uncaught (in promise) Objectmessage: "The message port closed before a response was received."[[Prototype]]: Object
BatchInsights:59 Uncaught ReferenceError: hub is not defined
    at HTMLDocument.<anonymous> (BatchInsights:59:1560)
    at i (jquery.min.js:2:27466)
    at Object.fireWith [as resolveWith] (jquery.min.js:2:28230)
    at Function.ready (jquery.min.js:2:30023)
    at HTMLDocument.K (jquery.min.js:2:30385)
jquery.simulate.js:331 Uncaught ReferenceError: jQuery is not defined
    at jquery.simulate.js:331:5

I followed the demo and still get this error ...here is the view

@model IEnumerable<FMM.Core.DataModel.Pharmpix.LogModel>
@using Kendo.Mvc.UI;
@using Kendo.Mvc.Extensions;
@using FMM.Core.DataModel.Pharmpix;

@{
    ViewData["Title"] = $"Batch Insights Logs";
    Layout = "~/Views/Shared/_Layout.cshtml";


}


<div>
    @(Html.Kendo().Notification()
        .Name("notification")
        .Width("100%")
        .Position(position => position
        .Top(50)
        .Left(50))
        )

   @(Html.Kendo().Grid(Model)
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.Id);
            columns.Bound(p => p.Logger).Width(150);
            columns.Bound(p => p.Message).Width(120).Filterable(false);

        })
        .ToolBar(toolbar =>
        {
            toolbar.Search();
        })
        .Groupable()
        .Pageable()
        .Editable(editable => editable.Mode(GridEditMode.InLine))
        .Scrollable()
        .HtmlAttributes(new { style = "height:100%;" })
        .DataSource(dataSource => dataSource
            .SignalR()
            .AutoSync(true)
            .PageSize(20)
            .Transport(tr => tr
                .Promise("hubStart")
                .Hub("hub")
                .Client(c => c
                    .Read("read")
                    )
                .Server(s => s
                    .Read("read")
               ))
                .Schema(schema => schema
                .Model(model =>
                {
                    model.Id("Id");
                    model.Field("Logger", typeof(string)).Editable(false);
                    model.Field("Message", typeof(string));

                })
            )
        ))



</div>


<script src="https://unpkg.com/@@aspnet/signalr@1.0.0/dist/browser/signalr.js"></script>
<script>
    $(document).ready(function () {
        var hubUrl = "logHub";
        var hub = new signalR.HubConnectionBuilder()
            .withUrl(hubUrl, {
                transport: signalR.HttpTransportType.LongPolling
            })
            .build();

        var hubStart = hub.start();
    });

    function onPush(e) {
        var notification = $("#notification").data("kendoNotification");
        notification.success(e.type);
    }
</script>

 

here is the hub

using FMM.Core.DataModel.Pharmpix;
using FMM.Core.Interfaces;
using FMM.DataAccess;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Financial_Management_Module.Hubs
{
    public class LogHub : Hub
    {
        private readonly PharmpixDbContext _context;
        private readonly IConfiguration _configuration;
        private readonly ILogging _log;

        public LogHub(PharmpixDbContext context, IConfiguration configuration, ILogging log)
        {
            _context = context;
            _configuration = configuration;
            _log = log;

        }



        public override System.Threading.Tasks.Task OnConnectedAsync()
        {
            Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName());
            return base.OnConnectedAsync();
        }

        public override System.Threading.Tasks.Task OnDisconnectedAsync(Exception e)
        {
            Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGroupName());
            return base.OnDisconnectedAsync(e);
        }


        public IEnumerable<LogModel> Read()
        {
            var result = _context.LogModel.Select(i => new LogModel
            {
                Id = i.Id,
                Logger = i.Logger,
                Message = i.Message
            }
            
            ).ToList();


            return result;

        }



        public string GetGroupName()
        {
            return GetRemoteIpAddress();
        }

        public string GetRemoteIpAddress()
        {
            return Context.GetHttpContext()?.Connection.RemoteIpAddress.ToString();
        }



    }
}

 

here is the log hub 

 

using FMM.Core.DataModel.Pharmpix;
using FMM.Core.Interfaces;
using FMM.DataAccess;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Financial_Management_Module.Hubs
{
    public class LogHub : Hub
    {
        private readonly PharmpixDbContext _context;
        private readonly IConfiguration _configuration;
        private readonly ILogging _log;

        public LogHub(PharmpixDbContext context, IConfiguration configuration, ILogging log)
        {
            _context = context;
            _configuration = configuration;
            _log = log;

        }



        public override System.Threading.Tasks.Task OnConnectedAsync()
        {
            Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName());
            return base.OnConnectedAsync();
        }

        public override System.Threading.Tasks.Task OnDisconnectedAsync(Exception e)
        {
            Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGroupName());
            return base.OnDisconnectedAsync(e);
        }


        public IEnumerable<LogModel> Read()
        {
            var result = _context.LogModel.Select(i => new LogModel
            {
                Id = i.Id,
                Logger = i.Logger,
                Message = i.Message
            }
            
            ).ToList();


            return result;

        }



        public string GetGroupName()
        {
            return GetRemoteIpAddress();
        }

        public string GetRemoteIpAddress()
        {
            return Context.GetHttpContext()?.Connection.RemoteIpAddress.ToString();
        }



    }
}

the code gets called if i comment out the "signal" stuff but when new entries are inserted it doesnt up date the the grid any help is appreciated!

 

                                              

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 17 Jan 2024, 06:10 PM

Hi Mark,

Thank you for providing code snippets.

I don't see any obvious causes of the reported errors based on the code you have shared.

Therefore, I have created a Grid SignalR example at the public Telerik.Examples GitHub Repo, you can compare the code, clone the repo and run it on your side and most notably you can use the commit changes' notes to create a small isolated sample project that uses Grid with SignalR.

Please consider replicating the observed issue within such an isolated app and send it back to me to investigate further. Enabling me to observe the behavior seen on your side will allow me to debug the issue locally and suggest a possible solution.

Regards,
Stoyan
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or