Go V4 Misc API Reference for Realtime Apps
The methods on this page are utility methods that don't fit into other categories.
Decrypt File
Description
This function allow to decrypt
the file content/data.
Method(s)
To decrypt
the file you can use the following method(s) in Go V4 SDK.
utils.DecryptFile(string, int64, io.Reader, io.WriteCloser)
Parameter Type Required Description cipherKey
String Optional Cipher Key to use for decryption. If no key is provided, CipherKey provided in PNConfiguration
will be considered.contentLenEnc
int64 Yes The total length of the file
.reader
io.Reader Yes Reader to read the encrypted contents. w
io.WriteCloser Yes Writer to write the decrypted contents.
Basic Usage
outDec, _ := os.Open("cat_picture.jpg")
fi, _ := outDec.Stat()
contentLenEnc := fi.Size()
defer outDec.Close()
fileDec, _ := os.Create(filepathOutputDec)
defer fileDec.Close()
r, w := io.Pipe()
utils.DecryptFile("enigma", contentLenEnc, outDec, w)
io.Copy(fileDec, r)
Encrypt File
Description
This function allow to encrypt
the file content/data.
Method(s)
To encrypt
the file you can use the following method(s) in Go V4 SDK.
utils.EncryptFile(string, []byte, io.Writer, os.File)
Parameter Type Required Description cipherKey
String Optional Cipher Key to use for encryption. If no key is provided, CipherKey provided in PNConfiguration
will be considered.iv
string Optional Initalization Vector if using a hardcoded one (not recommended). Should be passed as []byte{}
when not used.filePart
io.Writer Yes Writer to write the encrypted contents. file
os.File Yes Reader to read the file
.
Basic Usage
out, _ := os.Create("cat_picture.jpg")
file, err := os.Open(filepathInput)
if err != nil {
panic(err)
}
utils.EncryptFile("enigma", []byte{}, out, file)
Time
Description
This function will return a 17 digit precision Unix epoch.
The timetoken is constructed using the following algorithm:
timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time and date:
08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000
Method(s)
To fetch Time
you can use the following method(s) in Go V4 SDK:
pn.Time().QueryParam(queryParam).Execute()
Parameter Type Required Description QueryParam
map[string]string Optional QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
Basic Usage
res, status, err := pn.Time().Execute()
fmt.Println(res, status, err)
Response
Method | Type | Description |
---|---|---|
Timetoken | int64 | Returns a date representation of current timetoken. |
Create Push Payload
Description
This method creates the push payload for use in the appropriate endpoint calls.
Method(s)
CreatePushPayload().SetAPNSPayload(pubnub.PNAPNSData, []pubnub.PNAPNS2Data).SetCommonPayload(map[string]interface{}).SetFCMPayload(pubnub.PNFCMData).BuildPayload()
Parameter Type Required Description SetAPNSPayload
pubnub.PNAPNSData Optional Set APNS Payload. Associated APNS devices will receive only the data within the pn_apns
key.SetAPNSPayload
[]pubnub.PNAPNS2Data Optional Set APNS2 Payload. Associated APNS devices will receive only the data within the pn_push
key.SetFCMPayload
pubnub.PNFCMData Optional Set FCM Payload. Associated FCM devices will receive only the data within the pn_gcm
key.SetCommonPayload
map[string]interface{} Optional Set Common Payload. Native PubNub subscribers will receive the entire object literal, including the pn_apns
,pn_gcm
, andcommon payload
.BuildPayload
Yes Builds the payload from the values set using the parameters. Returns a map[string]interface{}
Basic Usage
Create Push Payload
aps := pubnub.PNAPSData{
Alert: "apns alert",
Badge: 1,
Sound: "ding",
Custom: map[string]interface{}{
"aps_key1": "aps_value1",
"aps_key2": "aps_value2",
},
}
apns := pubnub.PNAPNSData{
APS: aps,
Custom: map[string]interface{}{
"apns_key1": "apns_value1",
"apns_key2": "apns_value2",
},
}
apns2One := pubnub.PNAPNS2Data{
CollapseID: "invitations",
Expiration: "2019-12-13T22:06:09Z",
Version: "v1",
Targets: []pubnub.PNPushTarget{
pubnub.PNPushTarget{
Environment: pubnub.PNPushEnvironmentDevelopment,
Topic: "com.meetings.chat.app",
ExcludeDevices: []string{
"device1",
"device2",
},
},
},
}
apns2Two := pubnub.PNAPNS2Data{
CollapseID: "invitations",
Expiration: "2019-12-15T22:06:09Z",
Version: "v2",
Targets: []pubnub.PNPushTarget{
pubnub.PNPushTarget{
Environment: pubnub.PNPushEnvironmentProduction,
Topic: "com.meetings.chat.app",
ExcludeDevices: []string{
"device3",
"device4",
},
},
},
}
apns2 := []pubnub.PNAPNS2Data{apns2One, apns2Two}
fcm := pubnub.PNFCMData{
Data: pubnub.PNFCMDataFields{
Summary: "summary",
Custom: map[string]interface{}{
"fcm_data_key1": "fcm_data_value1",
"fcm_data_key2": "fcm_data_value2",
},
},
Custom: map[string]interface{}{
"fcm_key1": "fcm_value1",
"fcm_key2": "fcm_value2",
},
}
CommonPayload := map[string]interface{}{
"a": map[string]interface{}{
"common_key1": "common_value1",
"common_key2": "common_value2",
},
"b": "val",
}
s := pn.CreatePushPayload().SetAPNSPayload(apns, apns2).SetCommonPayload(CommonPayload).SetFCMPayload(fcm).BuildPayload()
res, status, err := pn.Publish().
Channel("my-channel").
Message(s).
Execute()
fmt.Println(res, status, err)
Response
The CreatePushPayload()
operation returns a map[string]interface{}
which can be passed directly to the Publish
Method's Message
parameter.