forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubnub.go
790 lines (643 loc) · 32.3 KB
/
pubnub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package pubnub
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime"
"sync"
)
// Default constants
const (
// Version :the version of the SDK
Version = "4.9.0"
// MaxSequence for publish messages
MaxSequence = 65535
)
const (
// StrMissingPubKey shows Missing Publish Key message
StrMissingPubKey = "Missing Publish Key"
// StrMissingSubKey shows Missing Subscribe Key message
StrMissingSubKey = "Missing Subscribe Key"
// StrMissingChannel shows Channel message
StrMissingChannel = "Missing Channel"
// StrMissingChannelGroup shows Channel Group message
StrMissingChannelGroup = "Missing Channel Group"
// StrMissingMessage shows Missing Message message
StrMissingMessage = "Missing Message"
// StrMissingSecretKey shows Missing Secret Key message
StrMissingSecretKey = "Missing Secret Key"
// StrMissingUUID shows Missing UUID message
StrMissingUUID = "Missing UUID"
// StrMissingDeviceID shows Missing Device ID message
StrMissingDeviceID = "Missing Device ID"
// StrMissingPushType shows Missing Push Type message
StrMissingPushType = "Missing Push Type"
// StrMissingPushTopic shows Missing Push Topic message
StrMissingPushTopic = "Missing Push Topic"
// StrChannelsTimetoken shows Missing Channels Timetoken message
StrChannelsTimetoken = "Missing Channels Timetoken"
// StrChannelsTimetokenLength shows Length of Channels Timetoken message
StrChannelsTimetokenLength = "Length of Channels Timetoken and Channels do not match"
// StrInvalidTTL shows Invalid TTL message
StrInvalidTTL = "Invalid TTL"
// StrMissingPushTitle shows `Push title missing` message
StrMissingPushTitle = "Push title missing"
// StrMissingFileID shows `Missing File ID` message
StrMissingFileID = "Missing File ID"
// StrMissingFileName shows `Missing File Name` message
StrMissingFileName = "Missing File Name"
)
// PubNub No server connection will be established when you create a new PubNub object.
// To establish a new connection use Subscribe() function of PubNub type.
type PubNub struct {
sync.RWMutex
Config *Config
nextPublishSequence int
publishSequenceMutex sync.RWMutex
subscriptionManager *SubscriptionManager
telemetryManager *TelemetryManager
heartbeatManager *HeartbeatManager
client *http.Client
subscribeClient *http.Client
requestWorkers *RequestWorkers
jobQueue chan *JobQItem
ctx Context
cancel func()
tokenManager *TokenManager
}
// Publish is used to send a message to all subscribers of a channel.
func (pn *PubNub) Publish() *publishBuilder {
return newPublishBuilder(pn)
}
// PublishWithContext function is used to send a message to all subscribers of a channel.
func (pn *PubNub) PublishWithContext(ctx Context) *publishBuilder {
return newPublishBuilderWithContext(pn, ctx)
}
// Fire endpoint allows the client to send a message to PubNub Functions Event Handlers. These messages will go directly to any Event Handlers registered on the channel that you fire to and will trigger their execution.
func (pn *PubNub) Fire() *fireBuilder {
return newFireBuilder(pn)
}
// FireWithContext endpoint allows the client to send a message to PubNub Functions Event Handlers. These messages will go directly to any Event Handlers registered on the channel that you fire to and will trigger their execution.
func (pn *PubNub) FireWithContext(ctx Context) *fireBuilder {
return newFireBuilderWithContext(pn, ctx)
}
// Subscribe causes the client to create an open TCP socket to the PubNub Real-Time Network and begin listening for messages on a specified channel.
func (pn *PubNub) Subscribe() *subscribeBuilder {
return newSubscribeBuilder(pn)
}
// History fetches historical messages of a channel.
func (pn *PubNub) History() *historyBuilder {
return newHistoryBuilder(pn)
}
// HistoryWithContext fetches historical messages of a channel.
func (pn *PubNub) HistoryWithContext(ctx Context) *historyBuilder {
return newHistoryBuilderWithContext(pn, ctx)
}
// Fetch fetches historical messages from multiple channels.
func (pn *PubNub) Fetch() *fetchBuilder {
return newFetchBuilder(pn)
}
// FetchWithContext fetches historical messages from multiple channels.
func (pn *PubNub) FetchWithContext(ctx Context) *fetchBuilder {
return newFetchBuilderWithContext(pn, ctx)
}
// MessageCounts Returns the number of messages published on one or more channels since a given time.
func (pn *PubNub) MessageCounts() *messageCountsBuilder {
return newMessageCountsBuilder(pn)
}
// MessageCountsWithContext Returns the number of messages published on one or more channels since a given time.
func (pn *PubNub) MessageCountsWithContext(ctx Context) *messageCountsBuilder {
return newMessageCountsBuilderWithContext(pn, ctx)
}
// GetAllUUIDMetadata Returns a paginated list of UUID Metadata objects, optionally including the custom data object for each.
func (pn *PubNub) GetAllUUIDMetadata() *getAllUUIDMetadataBuilder {
return newGetAllUUIDMetadataBuilder(pn)
}
// GetAllUUIDMetadataWithContext Returns a paginated list of UUID Metadata objects, optionally including the custom data object for each.
func (pn *PubNub) GetAllUUIDMetadataWithContext(ctx Context) *getAllUUIDMetadataBuilder {
return newGetAllUUIDMetadataBuilderWithContext(pn, ctx)
}
// GetUUIDMetadata Returns metadata for the specified UUID, optionally including the custom data object for each.
func (pn *PubNub) GetUUIDMetadata() *getUUIDMetadataBuilder {
return newGetUUIDMetadataBuilder(pn)
}
// GetUUIDMetadataWithContext Returns metadata for the specified UUID, optionally including the custom data object for each.
func (pn *PubNub) GetUUIDMetadataWithContext(ctx Context) *getUUIDMetadataBuilder {
return newGetUUIDMetadataBuilderWithContext(pn, ctx)
}
// SetUUIDMetadata Set metadata for a UUID in the database, optionally including the custom data object for each.
func (pn *PubNub) SetUUIDMetadata() *setUUIDMetadataBuilder {
return newSetUUIDMetadataBuilder(pn)
}
// SetUUIDMetadataWithContext Set metadata for a UUID in the database, optionally including the custom data object for each.
func (pn *PubNub) SetUUIDMetadataWithContext(ctx Context) *setUUIDMetadataBuilder {
return newSetUUIDMetadataBuilderWithContext(pn, ctx)
}
// RemoveUUIDMetadata Removes the metadata from a specified UUID.
func (pn *PubNub) RemoveUUIDMetadata() *removeUUIDMetadataBuilder {
return newRemoveUUIDMetadataBuilder(pn)
}
// RemoveUUIDMetadataWithContext Removes the metadata from a specified UUID.
func (pn *PubNub) RemoveUUIDMetadataWithContext(ctx Context) *removeUUIDMetadataBuilder {
return newRemoveUUIDMetadataBuilderWithContext(pn, ctx)
}
// GetAllChannelMetadata Returns a paginated list of Channel Metadata objects, optionally including the custom data object for each.
func (pn *PubNub) GetAllChannelMetadata() *getAllChannelMetadataBuilder {
return newGetAllChannelMetadataBuilder(pn)
}
// GetAllChannelMetadataWithContext Returns a paginated list of Channel Metadata objects, optionally including the custom data object for each.
func (pn *PubNub) GetAllChannelMetadataWithContext(ctx Context) *getAllChannelMetadataBuilder {
return newGetAllChannelMetadataBuilderWithContext(pn, ctx)
}
// GetChannelMetadata Returns metadata for the specified Channel, optionally including the custom data object for each.
func (pn *PubNub) GetChannelMetadata() *getChannelMetadataBuilder {
return newGetChannelMetadataBuilder(pn)
}
// GetChannelMetadataWithContext Returns metadata for the specified Channel, optionally including the custom data object for each.
func (pn *PubNub) GetChannelMetadataWithContext(ctx Context) *getChannelMetadataBuilder {
return newGetChannelMetadataBuilderWithContext(pn, ctx)
}
// SetChannelMetadata Set metadata for a Channel in the database, optionally including the custom data object for each.
func (pn *PubNub) SetChannelMetadata() *setChannelMetadataBuilder {
return newSetChannelMetadataBuilder(pn)
}
// SetChannelMetadataWithContext Set metadata for a Channel in the database, optionally including the custom data object for each.
func (pn *PubNub) SetChannelMetadataWithContext(ctx Context) *setChannelMetadataBuilder {
return newSetChannelMetadataBuilderWithContext(pn, ctx)
}
// RemoveChannelMetadata Removes the metadata from a specified channel.
func (pn *PubNub) RemoveChannelMetadata() *removeChannelMetadataBuilder {
return newRemoveChannelMetadataBuilder(pn)
}
// RemoveChannelMetadataWithContext Removes the metadata from a specified channel.
func (pn *PubNub) RemoveChannelMetadataWithContext(ctx Context) *removeChannelMetadataBuilder {
return newRemoveChannelMetadataBuilderWithContext(pn, ctx)
}
// GetMemberships The method returns a list of channel memberships for a user. This method doesn't return a user's subscriptions.
func (pn *PubNub) GetMemberships() *getMembershipsBuilderV2 {
return newGetMembershipsBuilderV2(pn)
}
// GetMembershipsWithContext The method returns a list of channel memberships for a user. This method doesn't return a user's subscriptions.
func (pn *PubNub) GetMembershipsWithContext(ctx Context) *getMembershipsBuilderV2 {
return newGetMembershipsBuilderV2WithContext(pn, ctx)
}
// GetChannelMembers The method returns a list of members in a channel. The list will include user metadata for members that have additional metadata stored in the database.
func (pn *PubNub) GetChannelMembers() *getChannelMembersBuilderV2 {
return newGetChannelMembersBuilderV2(pn)
}
// GetChannelMembersWithContext The method returns a list of members in a channel. The list will include user metadata for members that have additional metadata stored in the database.
func (pn *PubNub) GetChannelMembersWithContext(ctx Context) *getChannelMembersBuilderV2 {
return newGetChannelMembersBuilderV2WithContext(pn, ctx)
}
// SetChannelMembers This method sets members in a channel.
func (pn *PubNub) SetChannelMembers() *setChannelMembersBuilder {
return newSetChannelMembersBuilder(pn)
}
// SetChannelMembersWithContext This method sets members in a channel.
func (pn *PubNub) SetChannelMembersWithContext(ctx Context) *setChannelMembersBuilder {
return newSetChannelMembersBuilderWithContext(pn, ctx)
}
// RemoveChannelMembers Remove members from a Channel.
func (pn *PubNub) RemoveChannelMembers() *removeChannelMembersBuilder {
return newRemoveChannelMembersBuilder(pn)
}
// RemoveChannelMembersWithContext Remove members from a Channel.
func (pn *PubNub) RemoveChannelMembersWithContext(ctx Context) *removeChannelMembersBuilder {
return newRemoveChannelMembersBuilderWithContext(pn, ctx)
}
// SetMemberships Set channel memberships for a UUID.
func (pn *PubNub) SetMemberships() *setMembershipsBuilder {
return newSetMembershipsBuilder(pn)
}
// SetMembershipsWithContext Set channel memberships for a UUID.
func (pn *PubNub) SetMembershipsWithContext(ctx Context) *setMembershipsBuilder {
return newSetMembershipsBuilderWithContext(pn, ctx)
}
// RemoveMemberships Remove channel memberships for a UUID.
func (pn *PubNub) RemoveMemberships() *removeMembershipsBuilder {
return newRemoveMembershipsBuilder(pn)
}
// RemoveMembershipsWithContext Remove channel memberships for a UUID.
func (pn *PubNub) RemoveMembershipsWithContext(ctx Context) *removeMembershipsBuilder {
return newRemoveMembershipsBuilderWithContext(pn, ctx)
}
// ManageChannelMembers The method Set and Remove channel memberships for a user.
func (pn *PubNub) ManageChannelMembers() *manageChannelMembersBuilderV2 {
return newManageChannelMembersBuilderV2(pn)
}
// ManageChannelMembersWithContext The method Set and Remove channel memberships for a user.
func (pn *PubNub) ManageChannelMembersWithContext(ctx Context) *manageChannelMembersBuilderV2 {
return newManageChannelMembersBuilderV2WithContext(pn, ctx)
}
// ManageMemberships Manage the specified UUID's memberships. You can Add, Remove, and Update a UUID's memberships.
func (pn *PubNub) ManageMemberships() *manageMembershipsBuilderV2 {
return newManageMembershipsBuilderV2(pn)
}
// ManageMembershipsWithContext Manage the specified UUID's memberships. You can Add, Remove, and Update a UUID's memberships.
func (pn *PubNub) ManageMembershipsWithContext(ctx Context) *manageMembershipsBuilderV2 {
return newManageMembershipsBuilderV2WithContext(pn, ctx)
}
// Signal The signal() function is used to send a signal to all subscribers of a channel.
func (pn *PubNub) Signal() *signalBuilder {
return newSignalBuilder(pn)
}
// SignalWithContext The signal() function is used to send a signal to all subscribers of a channel.
func (pn *PubNub) SignalWithContext(ctx Context) *signalBuilder {
return newSignalBuilderWithContext(pn, ctx)
}
// SetState The state API is used to set/get key/value pairs specific to a subscriber UUID. State information is supplied as a JSON object of key/value pairs.
func (pn *PubNub) SetState() *setStateBuilder {
return newSetStateBuilder(pn)
}
// SetStateWithContext The state API is used to set/get key/value pairs specific to a subscriber UUID. State information is supplied as a JSON object of key/value pairs.
func (pn *PubNub) SetStateWithContext(ctx Context) *setStateBuilder {
return newSetStateBuilderWithContext(pn, ctx)
}
// Grant This function establishes access permissions for PubNub Access Manager (PAM) by setting the read or write attribute to true. A grant with read or write set to false (or not included) will revoke any previous grants with read or write set to true.
func (pn *PubNub) Grant() *grantBuilder {
return newGrantBuilder(pn)
}
// GrantWithContext This function establishes access permissions for PubNub Access Manager (PAM) by setting the read or write attribute to true. A grant with read or write set to false (or not included) will revoke any previous grants with read or write set to true.
func (pn *PubNub) GrantWithContext(ctx Context) *grantBuilder {
return newGrantBuilderWithContext(pn, ctx)
}
// GrantToken Use the Grant Token method to generate an auth token with embedded access control lists. The client sends the auth token to PubNub along with each request.
func (pn *PubNub) GrantToken() *grantTokenBuilder {
return newGrantTokenBuilder(pn)
}
// GrantTokenWithContext Use the Grant Token method to generate an auth token with embedded access control lists. The client sends the auth token to PubNub along with each request.
func (pn *PubNub) GrantTokenWithContext(ctx Context) *grantTokenBuilder {
return newGrantTokenBuilderWithContext(pn, ctx)
}
// AddMessageAction Add an action on a published message. Returns the added action in the response.
func (pn *PubNub) AddMessageAction() *addMessageActionsBuilder {
return newAddMessageActionsBuilder(pn)
}
// AddMessageActionWithContext Add an action on a published message. Returns the added action in the response.
func (pn *PubNub) AddMessageActionWithContext(ctx Context) *addMessageActionsBuilder {
return newAddMessageActionsBuilderWithContext(pn, ctx)
}
// GetMessageActions Get a list of message actions in a channel. Returns a list of actions in the response.
func (pn *PubNub) GetMessageActions() *getMessageActionsBuilder {
return newGetMessageActionsBuilder(pn)
}
// GetMessageActionsWithContext Get a list of message actions in a channel. Returns a list of actions in the response.
func (pn *PubNub) GetMessageActionsWithContext(ctx Context) *getMessageActionsBuilder {
return newGetMessageActionsBuilderWithContext(pn, ctx)
}
// RemoveMessageAction Remove a peviously added action on a published message. Returns an empty response.
func (pn *PubNub) RemoveMessageAction() *removeMessageActionsBuilder {
return newRemoveMessageActionsBuilder(pn)
}
// RemoveMessageActionWithContext Remove a peviously added action on a published message. Returns an empty response.
func (pn *PubNub) RemoveMessageActionWithContext(ctx Context) *removeMessageActionsBuilder {
return newRemoveMessageActionsBuilderWithContext(pn, ctx)
}
// SetToken Stores a single token in the Token Management System for use in API calls.
func (pn *PubNub) SetToken(token string) {
pn.tokenManager.StoreToken(token)
}
// SetTokens Stores multiple tokens in the Token Management System for use in API calls.
func (pn *PubNub) SetTokens(tokens []string) {
pn.tokenManager.StoreTokens(tokens)
}
// GetTokens Returns the token for the specified resource type and ID. When no token is set for the resource ID, the method checks for a pattern token at the resource level. Returns nil if not found.
func (pn *PubNub) GetTokens() GrantResourcesWithPermissions {
return pn.tokenManager.GetAllTokens()
}
// GetTokensByResource Returns the token(s) for the specified type. Returns nil if not found.
func (pn *PubNub) GetTokensByResource(resourceType PNResourceType) GrantResourcesWithPermissions {
return pn.tokenManager.GetTokensByResource(resourceType)
}
// GetToken Returns the token for the specified resource type and ID. When no token is set for the resource ID, the method checks for a pattern token at the resource level. Returns nil if not found.
func (pn *PubNub) GetToken(resourceID string, resourceType PNResourceType) string {
return pn.tokenManager.GetToken(resourceID, resourceType)
}
// ResetTokenManager resets the token manager.
func (pn *PubNub) ResetTokenManager() {
pn.tokenManager.CleanUp()
}
// Unsubscribe When subscribed to a single channel, this function causes the client to issue a leave from the channel and close any open socket to the PubNub Network. For multiplexed channels, the specified channel(s) will be removed and the socket remains open until there are no more channels remaining in the list.
func (pn *PubNub) Unsubscribe() *unsubscribeBuilder {
return newUnsubscribeBuilder(pn)
}
// AddListener lets you add a new listener.
func (pn *PubNub) AddListener(listener *Listener) {
pn.subscriptionManager.AddListener(listener)
}
// RemoveListener lets you remove new listener.
func (pn *PubNub) RemoveListener(listener *Listener) {
pn.subscriptionManager.RemoveListener(listener)
}
// GetListeners gets all the existing isteners.
func (pn *PubNub) GetListeners() map[*Listener]bool {
return pn.subscriptionManager.GetListeners()
}
// Leave unsubscribes from a channel.
func (pn *PubNub) Leave() *leaveBuilder {
return newLeaveBuilder(pn)
}
// LeaveWithContext unsubscribes from a channel.
func (pn *PubNub) LeaveWithContext(ctx Context) *leaveBuilder {
return newLeaveBuilderWithContext(pn, ctx)
}
// Presence lets you subscribe to a presence channel.
func (pn *PubNub) Presence() *presenceBuilder {
return newPresenceBuilder(pn)
}
// PresenceWithContext lets you subscribe to a presence channel.
func (pn *PubNub) PresenceWithContext(ctx Context) *presenceBuilder {
return newPresenceBuilderWithContext(pn, ctx)
}
// Heartbeat You can send presence heartbeat notifications without subscribing to a channel. These notifications are sent periodically and indicate whether a client is connected or not.
func (pn *PubNub) Heartbeat() *heartbeatBuilder {
return newHeartbeatBuilder(pn)
}
// HeartbeatWithContext You can send presence heartbeat notifications without subscribing to a channel. These notifications are sent periodically and indicate whether a client is connected or not.
func (pn *PubNub) HeartbeatWithContext(ctx Context) *heartbeatBuilder {
return newHeartbeatBuilderWithContext(pn, ctx)
}
// SetClient Set a client for transactional requests (Non Subscribe).
func (pn *PubNub) SetClient(c *http.Client) {
pn.Lock()
pn.client = c
pn.Unlock()
}
// GetClient Get a client for transactional requests (Non Subscribe).
func (pn *PubNub) GetClient() *http.Client {
pn.Lock()
defer pn.Unlock()
if pn.client == nil {
if pn.Config.UseHTTP2 {
pn.client = NewHTTP2Client(pn.Config.ConnectTimeout,
pn.Config.SubscribeRequestTimeout)
} else {
pn.client = NewHTTP1Client(pn.Config.ConnectTimeout,
pn.Config.NonSubscribeRequestTimeout,
pn.Config.MaxIdleConnsPerHost)
}
}
return pn.client
}
// SetSubscribeClient Set a client for transactional requests.
func (pn *PubNub) SetSubscribeClient(client *http.Client) {
pn.Lock()
pn.subscribeClient = client
pn.Unlock()
}
// GetSubscribeClient Get a client for transactional requests.
func (pn *PubNub) GetSubscribeClient() *http.Client {
pn.Lock()
defer pn.Unlock()
if pn.subscribeClient == nil {
if pn.Config.UseHTTP2 {
pn.subscribeClient = NewHTTP2Client(pn.Config.ConnectTimeout,
pn.Config.SubscribeRequestTimeout)
} else {
pn.subscribeClient = NewHTTP1Client(pn.Config.ConnectTimeout,
pn.Config.SubscribeRequestTimeout, pn.Config.MaxIdleConnsPerHost)
}
}
return pn.subscribeClient
}
// GetSubscribedChannels gets a list of all subscribed channels.
func (pn *PubNub) GetSubscribedChannels() []string {
return pn.subscriptionManager.getSubscribedChannels()
}
// GetSubscribedGroups gets a list of all subscribed channel groups.
func (pn *PubNub) GetSubscribedGroups() []string {
return pn.subscriptionManager.getSubscribedGroups()
}
// UnsubscribeAll Unsubscribe from all channels and all channel groups.
func (pn *PubNub) UnsubscribeAll() {
pn.subscriptionManager.unsubscribeAll()
}
// ListPushProvisions Request for all channels on which push notification has been enabled using specified pushToken.
func (pn *PubNub) ListPushProvisions() *listPushProvisionsRequestBuilder {
return newListPushProvisionsRequestBuilder(pn)
}
// ListPushProvisionsWithContext Request for all channels on which push notification has been enabled using specified pushToken.
func (pn *PubNub) ListPushProvisionsWithContext(ctx Context) *listPushProvisionsRequestBuilder {
return newListPushProvisionsRequestBuilderWithContext(pn, ctx)
}
// AddPushNotificationsOnChannels Enable push notifications on provided set of channels.
func (pn *PubNub) AddPushNotificationsOnChannels() *addPushNotificationsOnChannelsBuilder {
return newAddPushNotificationsOnChannelsBuilder(pn)
}
// AddPushNotificationsOnChannelsWithContext Enable push notifications on provided set of channels.
func (pn *PubNub) AddPushNotificationsOnChannelsWithContext(ctx Context) *addPushNotificationsOnChannelsBuilder {
return newAddPushNotificationsOnChannelsBuilderWithContext(pn, ctx)
}
// RemovePushNotificationsFromChannels Disable push notifications on provided set of channels.
func (pn *PubNub) RemovePushNotificationsFromChannels() *removeChannelsFromPushBuilder {
return newRemoveChannelsFromPushBuilder(pn)
}
// RemovePushNotificationsFromChannelsWithContext Disable push notifications on provided set of channels.
func (pn *PubNub) RemovePushNotificationsFromChannelsWithContext(ctx Context) *removeChannelsFromPushBuilder {
return newRemoveChannelsFromPushBuilderWithContext(pn, ctx)
}
// RemoveAllPushNotifications Disable push notifications from all channels registered with the specified pushToken.
func (pn *PubNub) RemoveAllPushNotifications() *removeAllPushChannelsForDeviceBuilder {
return newRemoveAllPushChannelsForDeviceBuilder(pn)
}
// RemoveAllPushNotificationsWithContext Disable push notifications from all channels registered with the specified pushToken.
func (pn *PubNub) RemoveAllPushNotificationsWithContext(ctx Context) *removeAllPushChannelsForDeviceBuilder {
return newRemoveAllPushChannelsForDeviceBuilderWithContext(pn, ctx)
}
// AddChannelToChannelGroup This function adds a channel to a channel group.
func (pn *PubNub) AddChannelToChannelGroup() *addChannelToChannelGroupBuilder {
return newAddChannelToChannelGroupBuilder(pn)
}
// AddChannelToChannelGroupWithContext This function adds a channel to a channel group.
func (pn *PubNub) AddChannelToChannelGroupWithContext(ctx Context) *addChannelToChannelGroupBuilder {
return newAddChannelToChannelGroupBuilderWithContext(pn, ctx)
}
// RemoveChannelFromChannelGroup This function removes the channels from the channel group.
func (pn *PubNub) RemoveChannelFromChannelGroup() *removeChannelFromChannelGroupBuilder {
return newRemoveChannelFromChannelGroupBuilder(pn)
}
// RemoveChannelFromChannelGroupWithContext This function removes the channels from the channel group.
func (pn *PubNub) RemoveChannelFromChannelGroupWithContext(ctx Context) *removeChannelFromChannelGroupBuilder {
return newRemoveChannelFromChannelGroupBuilderWithContext(pn, ctx)
}
// DeleteChannelGroup This function removes the channel group.
func (pn *PubNub) DeleteChannelGroup() *deleteChannelGroupBuilder {
return newDeleteChannelGroupBuilder(pn)
}
// DeleteChannelGroupWithContext This function removes the channel group.
func (pn *PubNub) DeleteChannelGroupWithContext(ctx Context) *deleteChannelGroupBuilder {
return newDeleteChannelGroupBuilderWithContext(pn, ctx)
}
// ListChannelsInChannelGroup This function lists all the channels of the channel group.
func (pn *PubNub) ListChannelsInChannelGroup() *allChannelGroupBuilder {
return newAllChannelGroupBuilder(pn)
}
// ListChannelsInChannelGroupWithContext This function lists all the channels of the channel group.
func (pn *PubNub) ListChannelsInChannelGroupWithContext(ctx Context) *allChannelGroupBuilder {
return newAllChannelGroupBuilderWithContext(pn, ctx)
}
// GetState The state API is used to set/get key/value pairs specific to a subscriber UUID. State information is supplied as a JSON object of key/value pairs.
func (pn *PubNub) GetState() *getStateBuilder {
return newGetStateBuilder(pn)
}
// GetStateWithContext The state API is used to set/get key/value pairs specific to a subscriber UUID. State information is supplied as a JSON object of key/value pairs.
func (pn *PubNub) GetStateWithContext(ctx Context) *getStateBuilder {
return newGetStateBuilderWithContext(pn, ctx)
}
// HereNow You can obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel by calling the HereNow() function in your application.
func (pn *PubNub) HereNow() *hereNowBuilder {
return newHereNowBuilder(pn)
}
// HereNowWithContext You can obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel by calling the HereNow() function in your application.
func (pn *PubNub) HereNowWithContext(ctx Context) *hereNowBuilder {
return newHereNowBuilderWithContext(pn, ctx)
}
// WhereNow You can obtain information about the current list of channels to which a UUID is subscribed to by calling the WhereNow() function in your application.
func (pn *PubNub) WhereNow() *whereNowBuilder {
return newWhereNowBuilder(pn)
}
// WhereNowWithContext You can obtain information about the current list of channels to which a UUID is subscribed to by calling the WhereNow() function in your application.
func (pn *PubNub) WhereNowWithContext(ctx Context) *whereNowBuilder {
return newWhereNowBuilderWithContext(pn, ctx)
}
// Time This function will return a 17 digit precision Unix epoch.
func (pn *PubNub) Time() *timeBuilder {
return newTimeBuilder(pn)
}
// TimeWithContext This function will return a 17 digit precision Unix epoch.
func (pn *PubNub) TimeWithContext(ctx Context) *timeBuilder {
return newTimeBuilderWithContext(pn, ctx)
}
// CreatePushPayload This method creates the push payload for use in the appropriate endpoint calls.
func (pn *PubNub) CreatePushPayload() *publishPushHelperBuilder {
return newPublishPushHelperBuilder(pn)
}
// CreatePushPayloadWithContext This method creates the push payload for use in the appropriate endpoint calls.
func (pn *PubNub) CreatePushPayloadWithContext(ctx Context) *publishPushHelperBuilder {
return newPublishPushHelperBuilderWithContext(pn, ctx)
}
// DeleteMessages Removes the messages from the history of a specific channel.
func (pn *PubNub) DeleteMessages() *historyDeleteBuilder {
return newHistoryDeleteBuilder(pn)
}
// DeleteMessagesWithContext Removes the messages from the history of a specific channel.
func (pn *PubNub) DeleteMessagesWithContext(ctx Context) *historyDeleteBuilder {
return newHistoryDeleteBuilderWithContext(pn, ctx)
}
// SendFile Clients can use this SDK method to upload a file and publish it to other users in a channel.
func (pn *PubNub) SendFile() *sendFileBuilder {
return newSendFileBuilder(pn)
}
// SendFileWithContext Clients can use this SDK method to upload a file and publish it to other users in a channel.
func (pn *PubNub) SendFileWithContext(ctx Context) *sendFileBuilder {
return newSendFileBuilderWithContext(pn, ctx)
}
// ListFiles Provides the ability to fetch all files in a channel.
func (pn *PubNub) ListFiles() *listFilesBuilder {
return newListFilesBuilder(pn)
}
// ListFilesWithContext Provides the ability to fetch all files in a channel.
func (pn *PubNub) ListFilesWithContext(ctx Context) *listFilesBuilder {
return newListFilesBuilderWithContext(pn, ctx)
}
// GetFileURL gets the URL of the file.
func (pn *PubNub) GetFileURL() *getFileURLBuilder {
return newGetFileURLBuilder(pn)
}
// GetFileURLWithContext gets the URL of the file.
func (pn *PubNub) GetFileURLWithContext(ctx Context) *getFileURLBuilder {
return newGetFileURLBuilderWithContext(pn, ctx)
}
// DownloadFile Provides the ability to fetch an individual file.
func (pn *PubNub) DownloadFile() *downloadFileBuilder {
return newDownloadFileBuilder(pn)
}
// DownloadFileWithContext Provides the ability to fetch an individual file.
func (pn *PubNub) DownloadFileWithContext(ctx Context) *downloadFileBuilder {
return newDownloadFileBuilderWithContext(pn, ctx)
}
// DeleteFile Provides the ability to delete an individual file.
func (pn *PubNub) DeleteFile() *deleteFileBuilder {
return newDeleteFileBuilder(pn)
}
// DeleteFileWithContext Provides the ability to delete an individual file
func (pn *PubNub) DeleteFileWithContext(ctx Context) *deleteFileBuilder {
return newDeleteFileBuilderWithContext(pn, ctx)
}
// PublishFileMessage Provides the ability to publish the asccociated messages with the uploaded file in case of failure to auto publish. To be used only in the case of failure.
func (pn *PubNub) PublishFileMessage() *publishFileMessageBuilder {
return newPublishFileMessageBuilder(pn)
}
// PublishFileMessageWithContext Provides the ability to publish the asccociated messages with the uploaded file in case of failure to auto publish. To be used only in the case of failure.
func (pn *PubNub) PublishFileMessageWithContext(ctx Context) *publishFileMessageBuilder {
return newPublishFileMessageBuilderWithContext(pn, ctx)
}
// Destroy stops all open requests, removes listeners, closes heartbeats, and cleans up.
func (pn *PubNub) Destroy() {
pn.Config.Log.Println("Calling Destroy")
pn.cancel()
if pn.subscriptionManager != nil {
pn.subscriptionManager.Destroy()
pn.Config.Log.Println("after subscription manager Destroy")
}
pn.Config.Log.Println("calling subscriptionManager Destroy")
if pn.heartbeatManager != nil {
pn.heartbeatManager.Destroy()
pn.Config.Log.Println("after heartbeat manager Destroy")
}
pn.Config.Log.Println("After Destroy")
pn.Config.Log.Println("calling RemoveAllListeners")
pn.subscriptionManager.RemoveAllListeners()
pn.Config.Log.Println("after RemoveAllListeners")
close(pn.jobQueue)
pn.Config.Log.Println("after close jobQueue")
pn.requestWorkers.Close()
pn.Config.Log.Println("after close requestWorkers")
pn.tokenManager.CleanUp()
}
func (pn *PubNub) getPublishSequence() int {
pn.publishSequenceMutex.Lock()
defer pn.publishSequenceMutex.Unlock()
if pn.nextPublishSequence == MaxSequence {
pn.nextPublishSequence = 1
} else {
pn.nextPublishSequence++
}
return pn.nextPublishSequence
}
// NewPubNub instantiates a PubNub instance with default values.
func NewPubNub(pnconf *Config) *PubNub {
ctx, cancel := contextWithCancel(backgroundContext)
if pnconf.Log == nil {
pnconf.Log = log.New(ioutil.Discard, "", log.Ldate|log.Ltime|log.Lshortfile)
}
pnconf.Log.Println(fmt.Sprintf("PubNub Go v4 SDK: %s\npnconf: %v\n%s\n%s\n%s", Version, pnconf, runtime.Version(), runtime.GOARCH, runtime.GOOS))
pn := &PubNub{
Config: pnconf,
nextPublishSequence: 0,
ctx: ctx,
cancel: cancel,
}
pn.subscriptionManager = newSubscriptionManager(pn, ctx)
pn.heartbeatManager = newHeartbeatManager(pn, ctx)
pn.telemetryManager = newTelemetryManager(pnconf.MaximumLatencyDataAge, ctx)
pn.jobQueue = make(chan *JobQItem)
pn.requestWorkers = pn.newNonSubQueueProcessor(pnconf.MaxWorkers, ctx)
pn.tokenManager = newTokenManager(pn, ctx)
return pn
}
func (pn *PubNub) newNonSubQueueProcessor(maxWorkers int, ctx Context) *RequestWorkers {
workers := make(chan chan *JobQItem, maxWorkers)
pn.Config.Log.Printf("Init RequestWorkers: workers %d", maxWorkers)
p := &RequestWorkers{
WorkersChannel: workers,
MaxWorkers: maxWorkers,
}
p.Start(pn, ctx)
return p
}
// NewPubNubDemo returns an instance with demo keys
func NewPubNubDemo() *PubNub {
return NewPubNub(NewDemoConfig())
}