pubnub / c-sharp Goto Github PK
View Code? Open in Web Editor NEWPubNub clients for C-Sharp based languages, including MS Windows C#/.net, Silveright, IIS, and Mono
License: Other
PubNub clients for C-Sharp based languages, including MS Windows C#/.net, Silveright, IIS, and Mono
License: Other
As per part 3.1.2 of Xamarin's Building Cross-Platform Applications document they say that libraries compiling against Mono for Android should use the ANDROID compiler directive, or ANDROID_version if they're targetting a specific version of the API. Similarly, Xamarin.iOS supports the IOS compiler directive, as seen in this Bugzilla ticket.
I'll submit a pull request for this.
Just upgraded the Pubnub from 3.18.4 to direct 6.19.4.
All went well but Fetch history having exeption on decryption.
Same thing worked for nuget 6.19.3.
After checking last commits for 6.19.4 I see exception at this place.
File: src/Api/PubnubApi/Security/SecureMessage.cs
Line number: 159
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : decryptMessage);
if there is DECRYPT ERROR, it should not be taking decryptMessage, it should be taking kvpValue.Value.ToString() as its done at other places.
I have an ASP.NET Core 6 application that's hosted in IIS. In the application I use PubNub 6.13.0
service to subscribe to a channel and listen/respond to incoming requests. The problem is the service stops listening after a while (usually a couple of days). The application works otherwise (handling REST and SignalR requests just fine), but the only way I can get PubNub to work is by Recycling the app pool.
I have the app deployed to about 20 servers at this point (both in AWS and on-prem), and this happens on every single instance. Any idea of what could potentially cause it, or how I could troubleshoot what's causing it, or at the very least hack some workaround? Thanks.
I initialize the service as singleton:
services.AddSingleton<IPubNubService, PubNubService>();
I 'start up' the instance on startup like this:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.ApplicationServices.GetService<IPubNubService>();
}
Here's a simplified view of the PubNubService
itself:
public class PubNubService : IPubNubService
{
public PubNubService()
{
_pubNub = new Pubnub(new PNConfiguration(new UserId($"PubNub:UserId"))
{
SubscribeKey = config.GetValue<string>("PubNub:SubscribeKey"),
PublishKey = config.GetValue<string>("PubNub:PublishKey"),
});
_pubNub.Subscribe<string>()
.Channels(new[] { "PubNub:BroadcastChannel" })
.Execute();
_pubNub.AddListener(CreateListener());
}
// https://github.com/pubnub/c-sharp/
// https://www.pubnub.com/docs/sdks/c-sharp/api-reference/configuration
private SubscribeCallbackExt CreateListener()
{
return new SubscribeCallbackExt
(
messageCallback: async delegate(Pubnub pnObj, PNMessageResult<object> pubMsg)
{
await HandleReceivedMessage(pubMsg);
},
);
}
rivate async Task HandleReceivedMessage(PNMessageResult<object> pubNubMessage)
{
// ...
}
}
While investigating an issue with high CPU usage, a memory dump was taken of a long-running ASP.NET Core 6 server application (see below) that uses the PubNubPCL library.
The heap has a lot of retained instances of objects related to the PubNub API, particularly PNConfguration
which, after about 3 days of continuous operation, occupies 107MB of heap space.
I suspect the issue lies with the lifetime of the PubNub client in the DI container. We have a thin wrapper around the PubNub API that we inject with a Transient lifetime. The constructor creates a new PubNub object and configures it every time it is injected.
Based on the large number of PubnubApi.ConcurrentDictionary
objects also on the heap, I believe the PubNub client is supposed to have a Singleton lifetime, but I could not find any guidance in the documentation regarding DI containers.
Can you confirm the expected lifetime of the PubNub client? Is it expected to be a Singleton, or can it be constructed as needed in a long-running service?
Here is the relevant part of the heap analysis created with dotnet-dump
. The command used was dumpheap -stat -live
(statistical analysis of live objects only).
MT Count TotalSize Class Name
[snip]
00007f832b80f308 235225 7527200 PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Net.HttpWebRequest, System.Net.Requests]]
00007f83294d3a10 235225 7527200 PubnubApi.NewtonsoftJsonDotNet
00007f832b827460 1 7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Net.HttpWebRequest, System.Net.Requests]], PubnubPCL]][]
00007f832b826520 1 7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.EndPoint.TokenManager, PubnubPCL]][]
00007f832b8262a0 1 7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.IPubnubLog, PubnubPCL]][]
00007f832b826020 1 7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.PNConfiguration, PubnubPCL]][]
00007f83294d45b8 235225 13172600 PubnubApi.EndPoint.TokenManager
00007f8325678080 4495 13808408 System.Int32[]
00007f832b80b6a0 470451 15054432 PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Boolean, System.Private.CoreLib]]
00007f832b8276e0 2 15573600 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Boolean, System.Private.CoreLib]], PubnubPCL]][]
00007f8325e89150 708563 17005512 System.Threading.Timer
00007f8326aada58 708775 17010600 System.Threading.TimerHolder
00007f83255c5290 708988 17015712 System.Object
00007f832b826f38 235225 18818000 System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Net.HttpWebRequest, System.Net.Requests]]
00007f8325ba2890 17 33503088 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]][]
00007f83294d4898 708502 34008096 PubnubApi.EndPoint.TelemetryManager
00007f832a8c9368 470451 37636080 System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Boolean, System.Private.CoreLib]]
00007f8325abac58 38012 39857914 System.Byte[]
00007f83266827b8 708749 45359936 System.Threading.TimerCallback
00007f8326682678 708860 68050560 System.Threading.TimerQueueTimer
00007f832567d2e0 740815 68704692 System.String
00007f83294d2820 708502 107692304 PubnubApi.PNConfiguration
Total 7963348 objects
Hi,
instead of a PubNub.cs file and a reference of Newtonsoft.Json could you provide a proper NuGet package where PubNub.cs is in a lib, and Newtonsoft.Json is referenced from his own nuget package. The current packaging make little sense.
Thanks.
Nuget SDK PubnubPCL : 6.19.3
Our app has an online/offline feature, and we subscribe to individual chat when we go inside the chat.
On the opening of the app we subscribe to the self-presence channel userId-presence. so other users can check online/offline status by subscribing to its presence channel userId-presence-pnpres
The presence signal works well, and the HereNow call returns the right Online/Offline status based on subscription.
However when a user goes inside the chat and subscribes to a new channel chatroom1, it leaves presence to the old Presence channel userId-presence
This does not happen every time. If we Subscribe to a channel and Unsubscribe to a channel quickly or multiple times in quick manner it starts to give a leave signal in the presence channel.
Because of that, users seem offline at the other end, which makes the app not function well.
Hi,
I've added PubNub to a very simple Xamarin project, and when I do Publish, it crashes on my iPhone 4. Simulator works fine..
You can look at it here:
https://github.com/woutercx/PubNubDemo
In readme.md
https://github.com/pubnub/c-sharp/blob/master/README.md
you say:
Please ensure that in order to run on Mono the constant in the pubnub.cs file should be set to "true" OVERRIDE_TCP_KEEP_ALIVE = true;
How do I do this?
When i try use 2 instances of Pubnub class (one for my channel, one for channel of different company, so i need 2 subscribe keys) sometimes messages cannot be published. Publish function return "ok" but nothing happens.
In 6.0.8 release both GrantTokenOperation.AuthorizedUuid
and PNTokenPermissionMappingBase.Channels
are marked with [Obsolete]
attribute and it's recommended to use GrantTokenOperation.AuthorizedUserId
and PNTokenPermissionMappingBase.Spaces
accordingly. But neither Spaces
nor AuthorizedUserId
are taken into account when granting access token.
When using Spaces
instead of Channel
server returns 400
status code with error message This grant contains no permissions
. Usage of AuthorizedUserId
instead of AuthorizedUuid
results in the loss of information about the authorized user in access token (value of authorized_uuid
property of parsed access token is null
)
A .NET 8 service application that subscribes to messages from PubNub and, upon upgrade/restart, will occasionally get into a state where it cannot connect to PubNub until the application is restarted again.
ReconnectionPolicy
repeatedly fails to connect to PubNub.Subscribe().Channels("entity-A-notification-type1").Execute()
).ClientNetworkStatus.RefHttpClient = httpClientNetworkStatus
.PubnubApiPCL.csproj
, I caught this state where, somehow, there is a header with a null
key (.NET thread safety issue with HTTP headers?).I have attached a simple application (from a Windows system). You will need to populate SubscribeKey, AuthKey, and channels with real values. Afterwards, you can run the script run.bat
which should build and then re-start the application until the problem eventually reproduces; the output will indicate a failure to connect (ERROR Internet connection problem. Retrying connection Cat=PNNetworkIssuesCategory
). I usually have it reproduce within about 30 seconds.
ReproductionApp.zip
The application is fairly straightforward: create the Pubnub
instance, perform Subscribe().Channels(...).Execute()
multiple times, then wait about 2 seconds for connection messages before exiting.
Hi,
I thought this issue was already solved but apparently not. In testing I forced the SDK to use the wrong auth key to see what the retry behavior would be and to my surprise you are retrying on all 403s sent by Pubnub creating a huge bill if not careful:
Warning: DateTime: 12/07/2021 01:22:03, Inside SendRequestAndGetJsonResponseHttpClient
Warning: DateTime 12/07/2021 01:22:03 GetTimeWithHttpClient Resp OK
Warning: DateTime 12/07/2021 01:22:03, JSON= {"message":"Forbidden","payload":{"channels":["chanel"]},"error":true,"service":"Access Manager","status":403}
Warning: DateTime 12/07/2021 01:22:03, EXPONENTIAL timerInterval = 7
Warning: DateTime 12/07/2021 01:22:10 MultiplexExceptionHandlerTimerCallback
Warning: DateTime 12/07/2021 01:22:10 MultiplexInternalCallback timetoken = 0
Warning: DateTime 12/07/2021 01:22:10 CheckSocketConnect Entered
Warning: DateTime 12/07/2021 01:22:10 GetTimeWithHttpClient Resp OK
Warning: DateTime 12/07/2021 01:22:10, JSON= {"message":"Forbidden","payload":{"channels":["chanel"]},"error":true,"service":"Access Manager","status":403}
Warning: DateTime 12/07/2021 01:22:10, EXPONENTIAL timerInterval = 15
This is part of the log, I've removed personal information and channel names since I don't want that information on github. But as you can see it keeps retrying on 403s which makes no sense. How can an internal retry logic solve a 403 without being able to acquire keys? This should be left up to the developer.
I thought this was a known issue. I had a support call with Craig Connover when he was helping us build the app, and later with his successor. How come this hasn't been fixed?
The cause of memory leak is public class TelemetryManager : IDisposable
.
This class is created by public class Pubnub
is case of config.EnableTelemetry == true
but will never be disposed.
It can lead to significantly increasing of Number of Active Timers
and ultimately lead to memory leak.
Also, non-disposed timers can affect monitor tools (e.g. New Relic) -
My suggestion is:
IDisposable
interface to Pubnub
class.Dispose
method to clear this.TelemetryManager.Code Optimizations in Azure has identified a excessive Memory usage by the "PubnubApi.EndPoint.TelemetryManager.CleanupTelemetryData" method. It has 3 recommendations.
Description: A lot of CPU or Memory is being spent in Enumerable.ToDictionary().
Current Condition: 19% of your Memory was spent in Enumerable.ToDictionary called from PubnubApi.EndPoint.TelemetryManager.CleanupTelemetryData. We expected this value to be <2%.
Recommendations: Verify that Enumerable.ToDictionary() isn't being called for a few look-ups in the resulting Dictionary<K, V>. This can instead be accomplished simply by enumerating over the Enumerable.
**Description: ** Task.ScheduleAndStart is causing unusually high memory allocations.
Current Condition: 6% of your Memory was spent in Task.ScheduleAndStart called from PubnubApi.EndPoint.TelemetryManager.CleanupTelemetryData. We expected this value to be <1%.
Recommendations: Consider investigating why Task.ScheduleAndStart is causing higher than expected memory allocations.
**Description: ** Excessive string concatenation is causing lots of allocations and CPU consumption.
Current Condition: 3% of your Memory was spent in String.Concat called from PubnubApi.EndPoint.TelemetryManager.CleanupTelemetryData. We expected this value to be <1%.
Recommendations: Consider using cheaper alternatives such as String.Join or a StringBuilder instead.
The recursive subscribe in MultiChannelSubscribeRequest can ultimately lead to stack overflow.
I've replaced it in our app with a simple loop, and the chap seeing the error has reported no more problems.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.