Vue API & SDK Docs 1.0.1
Unsupported SDK
PubNub no longer supports this SDK, but you are welcome to contribute.
Get Code: Source
Get Code: NPM
npm install pubnub-vue --save
Hello World
PubNub Vue is a wrapper of the PubNub JavaScript SDK version 4 that adds a few extra features to simplify the integration with Vue:
- Trigger Events:
$pnGetMessage
,$pnGetPresence
,$pnGetStatus
will make your life easier in the Vue' World. - Autoload: An easy and fast way to recovery the history messages of your channel.
You can still use the native PubNub JavaScript SDK if you feel this will be more suitable for your situation.
Differences in usage with native Javascript SDK
PubNub Vue is a wrapper of PubNub Javascript which offers the same features and in the same way as in the Javascript. If you have experience using the Javascript SDK, the Vue SDK will be fast to learn.
To learn more about PubNub JavaScript features, refer to the PubNub JavaScript SDK documentation.
Hello World Example
<template>
<div id="app">
<ol>
<li v-for="msg in ch1">{{msg.message}}</li>
</ol>
<button v-on:click="push">push</button>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
ch1: this.$pnGetMessage('ch1'),
},
},
methods: {
push() {
this.$pnPublish({
channel: 'ch1',
message: Date.now()
},
(status, response) => console.log(status, response));
show all 24 linesHow to use PubNubVue
In order to get the integration between your Vue's Component and PubNub, PubNubVue will be the way to get this without any kind of difficulty or extra job when you need render data in your UI.
- An instance of
PubNubVue
can be spread over all components hierarchically using the instance by default or assigning one instance for each component. - Use the hook
mounted
for subscribing from a component. - Use the hook
created
for invoking the extended method:$pnGetInstance
in order to create new instances.
You can still use the native PubNub JavaScript SDK if you feel this will be more suitable for your situation.
Note
Always set the UUID
to uniquely identify the user or device that connects to PubNub. This UUID
should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID
can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
import Vue from 'vue';
import PubNubVue from 'pubnub-vue';
import App from './App';
Vue.use(PubNubVue, {
subscribeKey: 'YOUR SUBSCRIBE KEY HERE',
publishKey: 'YOUR PUBLISH KEY HERE'
});
new Vue({
el: '#app',
template: '<App/>',
components: {
App
},
show all 16 linesEvents
Another key feature of this SDK is the ability to use trigger events, in addition to pass callbacks, getting all messages through states and inject them directly in your UI of your Vue Component in an easy and fast way.
Triggering and listening to events for the subscribe method
With the trigger events you can find a way to get real time apps with PubNub Vue very fast, because it will be resolved the synchronization between the data received and the user interface through associating a channel to a reactive property to the root data object.
The trigger events are methods which encapsulate the events (message
, presence
and status
) from Javascript SDK with extra functionality to offer integration and get the process of development faster because will be resolved different scenarios which you can find when you are working with Vue.
Triggering the events:
$pnGetMessage
Use $pnGetMessage
to associate a channel subscribed to a reactive property and start to display messages as soon as these are received.
Parameter | Description |
---|---|
channel *Type: String Default: n/a | Channel which will be listened. |
callback Type: Function Default: n/a | Function which will be invoked as soon as received a new real time message. |
keepMessage Type: Number Default: n/a | Number of messages which are kept and rendered . |
instanceName Type: String Default: default | Instance to use into the component. |
<template>
<div id="app">
<ol>
<li v-for="msg in ch1">{{ msg.message }}</li>
</ol>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
ch1: this.$pnGetMessage('ch1'),
},
},
show all 22 lines$pnGetMessage
will receive a second parameter which is a callback function that is going to be invoked as soon as received a new real time message. This will allow to apply transformation or manipulate of somehow to each message or implement a custom mechanism to render these.
<template>
<div id="app">
<ol>
<li v-for="msg in ch1">{{ msg.message }}</li>
</ol>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
ch1: this.$pnGetMessage('ch1', this.receptor),
},
},
show all 27 linesWhen you are using $pnGetMessage
this going to keep the latest 100 messages received by default. But you can change this value when you attach the channel with $pnGetMessage
.
<template>
<div id="app">
<ol>
<li v-for="msg in ch1">{{ msg.message }}</li>
</ol>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
ch1: this.$pnGetMessage('ch1', null, 10),
},
},
show all 27 lines