zetapush-js
Frequently Asked Questions
Can I use zetapush-js in a NodeJS Context?
Yes, you have to install ~latest~ (>=3.2.0) version of zetapush-js enable NodeJS support.
const { WeakClient } = require('zetapush-js')
const NodeJSTransports = require('zetapush-cometd/lib/node/Transports')
// Create new ZetaPush Client
const client = new WeakClient({
transports: NodeJSTransports,
sandboxId: '<YOUR-SANDBOX-ID>'
})
// Add connection establised listener
client.onConnectionEstablished(() => {
console.log('onConnectionEstablished')
})
client.connect()
Can I use zetapush-js with TypeScript?
Yes, since 3.1.2 zetapush-js include typings definition.
How to use ES2015 features (const, class, arrow functions, ...) on my project today ?
ZetaPush source code and examples are written in plain ES2015, you can use Babel to transpile your code
How to subscribe and publish to a ZetaPush service ?
ZetaPush Client provide a function createService({ type, listener })
const client = new ZetaPush.Client({ ... })
// Create a service publisher mapping Stack service
const service = client.createService({
type: ZetaPush.services.Stack,
listener: {
// callback fired when a list message is fired by ZetaPush
list(message) {
console.log('list callback', message)
}
}
})
// Request a list
service.list({
stack: '<YOUR-STACK-ID>'
})
How force ZetaPush SDK to use https instead http ?
ZetaPush client allow you to declare a specific apiUrl
const client = new ZetaPush.Client({
forceHttps: true,
sandboxId: '<YOUR-SANDBOX-ID>'
})
How to use ZetaPush with my on-premise backend ?
ZetaPush client allow you to declare a specific apiUrl
const client = new ZetaPush.Client({
apiUrl: '<YOUR-ON-PREMISE-URL>',
sandboxId: '<YOUR-SANDBOX-ID>'
})
How to know my ZetaPush SDK version ?
ZetaPush SDK provide a top level constant VERSION
console.log(ZetaPush.VERSION)
How to change log level ?
ZetaPush Client provide a function setLogLevel
const client = new ZetaPush.Client({
sandboxId: '<YOUR-SANDBOX-ID>'
})
client.setLogLevel('debug')
How to force long polling instead of websocket ?
ZetaPush Client provide an option to specify active transports
const client = new ZetaPush.WeakClient({
sandboxId: 'Y1k3xBDc',
transports: [ZetaPush.TransportTypes.LONG_POLLING]
})
How ZetaPush client call my service without explicit deployment id ?
ZetaPush Client provide optional deployment id, according to the following convention ${ServiceType.toLowerCase()_0}
// Create service with implicit service deployment id
const service = client.createService({
type: ZetaPush.services.Stack,
listener: {
list(message) {
console.log('on stack list', message)
}
}
})
service.list({
stack: '<YOUR-STACK-ID>'
})
In the previous example, deployment id is stack_0
// Create service with explicit service deployment id
const service = client.createService({
deploymentId: 'stack_0',
type: ZetaPush.services.Stack,
listener: {
list(message) {
console.log('on stack list', message)
}
}
})
service.list({
stack: '<YOUR-STACK-ID>'
})
Migrating from ZetaPush 1 to 2
ZetaPush 2 is a ground-up rewrite of ZetaPush. This new version of ZetaPush had three basic goals:
- Similar API to other language implementations (Java, Swift, ...)
- Better maintenance
- Full CommonJS support
Configuration
ZetaPush 1
zp.init('<YOUR-SANDBOX-ID>')
ZetaPush 2
const client = new ZetaPush.Client({
sandboxId: '<YOUR-SANDBOX-ID>'
})
Authentication
ZetaPush 1
zp.init('<YOUR-SANDBOX-ID>')
const authent = new zp.authent.Simple('<YOUR-SIMPLE-DEPLOYMENT-ID>')
zp.onHandshake((message) => {
console.log('ZetaPush_Hanshake_Successful', message)
})
zp.onConnected((message) => {
if (message.successful) {
console.log('You are connected')
}
})
zp.connect(authent.getConnectionData('login', 'password', 'resource'))
ZetaPush 2
const client = new ZetaPush.Client({
credentials() {
return ZetaPush.Authentication.simple({
deploymentId: '<YOUR-SIMPLE-DEPLOYMENT-ID>',
login: 'login',
password: 'password'
})
},
resource: 'resource',
sandboxId: '<YOUR-SANDBOX-ID>'
})
zp.onSuccessfulHandshake((message) => {
console.log('ZetaPush_Hanshake_Successful', message)
})
client.onConnectionEstablished(() => {
console.log('You are connected')
})
client.connect()
Publish/Subscribe
ZetaPush 1
const service = new zp.service.Generic('<YOUR-STACK-DEPLOYMENT-ID>')
service.onError((message) => {
console.error('on stack error', message)
})
service.on('list', (message) => {
console.log('on stack list', message)
})
service.send('list', {
stack: '<YOUR-STACK-ID>'
})
ZetaPush 2
const service = client.createService({
deploymentId: '<YOUR-STACK-DEPLOYMENT-ID>'
type: ZetaPush.services.Stack,
listener: {
error(message) {
console.error('on stack error', message)
},
list(message) {
console.log('on stack list', message)
}
}
})
service.list({
stack: '<YOUR-STACK-ID>'
})
Optional deployment id
ZetaPush 2
provide optional deployment id, according to the following convention ${ServiceType.toLowerCase()_0}
// Create service with implicit service deployment id
const service = client.createService({
type: ZetaPush.services.Stack,
listener: {
list(message) {
console.log('on stack list', message)
}
}
})
service.list({
stack: '<YOUR-STACK-ID>'
})
In the previous example, deployment id is stack_0
// Create service with explicit service deployment id
const service = client.createService({
deploymentId: 'stack_0',
type: ZetaPush.services.Stack,
listener: {
list(message) {
console.log('on stack list', message)
}
}
})
service.list({
stack: '<YOUR-STACK-ID>'
})