I need to create group in my signalR hub but the Context.ConnectionId line is null:
public class MetricHub : Hub
{
public MetricHub()
{
Groups.Add(Context.ConnectionId, "Syslog");
Groups.Add(Context.ConnectionId, "Heatmap");
}
}
and this is the backgroundTicker class which uses MetricHub class:
using AutoMapper;
using Makbin.Data;
using Makbin.Web.Model;
using Makbin.Web.ViewModels;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Hosting;
namespace Makbin.Web.Hubs
{
public class test
{
public int id { get; set; }
public string name { get; set; }
}
public class BackgroundTicker : IRegisteredObject
{
private readonly MakbinRepository _repository;
private Timer devicesTimer;
private IHubContext hub;
public BackgroundTicker()
{
_repository = new MakbinRepository();
HostingEnvironment.RegisterObject(this);
hub = GlobalHost.ConnectionManager.GetHubContext<MetricHub>();
devicesTimer = new Timer(OnDevicesTimerElapsed, null,
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
}
private void OnDevicesTimerElapsed(object sender)
{
var result = _repository.Peripherals.Select(x => new { x.PeripheralId, x.Severity });
var finalResult = JsonConvert.SerializeObject(result, Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
hub.Clients.Group("Heatmap").broadcastMessage(DateTime.UtcNow.ToString(), finalResult);
}
public void Stop(bool immediate)
{
//deviceDetailTimer.Dispose();
devicesTimer.Dispose();
HostingEnvironment.UnregisterObject(this);
}
}
}
My purpose from this code is to define groups before sending any data. Here I have defined two groups: Syslog, Heatmap. Besides, my connection to the client is one-way (server to client).