@@ -2,43 +2,43 @@ import PubNub, { Subscription } from '../lib/types';
22
33// snippet.gettingStartedInitPubnub
44const pubnub = new PubNub ( {
5- publishKey : 'YOUR_PUBLISH_KEY' ,
6- subscribeKey : 'YOUR_SUBSCRIBE_KEY' ,
7- userId : 'YOUR_USER_ID'
5+ publishKey : 'YOUR_PUBLISH_KEY' ,
6+ subscribeKey : 'YOUR_SUBSCRIBE_KEY' ,
7+ userId : 'YOUR_USER_ID' ,
88} ) ;
99// snippet.end
1010
1111// snippet.gettingStartedEventListeners
1212// Add listener to handle messages, presence events, and connection status
1313pubnub . addListener ( {
14- message : function ( event : Subscription . Message ) {
15- // Handle message event
16- console . log ( "New message:" , event . message ) ;
17- // Format and display received message
18- const displayText = ( ( ) => {
19- if ( typeof event . message === 'object' && event . message && 'text' in event . message ) {
20- const messageObj = event . message as { text ?: string ; sender ?: string } ;
21- return `${ messageObj . sender || 'User' } : ${ messageObj . text } ` ;
22- } else {
23- return `Message: ${ JSON . stringify ( event . message ) } ` ;
24- }
25- } ) ( ) ;
26- console . log ( displayText ) ;
27- } ,
28- presence : function ( event : Subscription . Presence ) {
29- // Handle presence event
30- console . log ( "Presence event:" , event ) ;
31- console . log ( "Action:" , event . action ) ; // join, leave, timeout
32- console . log ( "Channel:" , event . channel ) ;
33- } ,
34- status : function ( event ) {
35- // Handle status event
36- if ( event . category === "PNConnectedCategory" ) {
37- console . log ( "Connected to PubNub chat!" ) ;
38- } else if ( event . category === "PNNetworkIssuesCategory" ) {
39- console . log ( "Connection lost. Attempting to reconnect..." ) ;
40- }
14+ message : function ( event : Subscription . Message ) {
15+ // Handle message event
16+ console . log ( 'New message:' , event . message ) ;
17+ // Format and display received message
18+ const displayText = ( ( ) => {
19+ if ( typeof event . message === 'object' && event . message && 'text' in event . message ) {
20+ const messageObj = event . message as { text ?: string ; sender ?: string } ;
21+ return `${ messageObj . sender || 'User' } : ${ messageObj . text } ` ;
22+ } else {
23+ return `Message: ${ JSON . stringify ( event . message ) } ` ;
24+ }
25+ } ) ( ) ;
26+ console . log ( displayText ) ;
27+ } ,
28+ presence : function ( event : Subscription . Presence ) {
29+ // Handle presence event
30+ console . log ( 'Presence event:' , event ) ;
31+ console . log ( 'Action:' , event . action ) ; // join, leave, timeout
32+ console . log ( 'Channel:' , event . channel ) ;
33+ } ,
34+ status : function ( event ) {
35+ // Handle status event
36+ if ( event . category === 'PNConnectedCategory' ) {
37+ console . log ( 'Connected to PubNub chat!' ) ;
38+ } else if ( event . category === 'PNNetworkIssuesCategory' ) {
39+ console . log ( 'Connection lost. Attempting to reconnect...' ) ;
4140 }
41+ } ,
4242} ) ;
4343// snippet.end
4444
@@ -48,7 +48,7 @@ const channel = pubnub.channel('hello_world');
4848
4949// Create a subscription
5050const subscription = channel . subscription ( {
51- receivePresenceEvents : true // to receive presence events
51+ receivePresenceEvents : true , // to receive presence events
5252} ) ;
5353
5454// Subscribe
@@ -58,26 +58,26 @@ subscription.subscribe();
5858// snippet.gettingStartedPublishMessages
5959// Function to publish a message
6060async function publishMessage ( text : string ) {
61- if ( ! text . trim ( ) ) return ;
62-
63- try {
64- const result = await pubnub . publish ( {
65- message : {
66- text : text ,
67- sender : pubnub . userId ,
68- time : new Date ( ) . toISOString ( )
69- } ,
70- channel : 'hello_world'
71- } ) ;
72- console . log ( `Message published with timetoken: ${ result . timetoken } ` ) ;
73- console . log ( `You: ${ text } ` ) ;
74- } catch ( error ) {
75- console . error ( `Publish failed: ${ error } ` ) ;
76- }
61+ if ( ! text . trim ( ) ) return ;
62+
63+ try {
64+ const result = await pubnub . publish ( {
65+ message : {
66+ text : text ,
67+ sender : pubnub . userId ,
68+ time : new Date ( ) . toISOString ( ) ,
69+ } ,
70+ channel : 'hello_world' ,
71+ } ) ;
72+ console . log ( `Message published with timetoken: ${ result . timetoken } ` ) ;
73+ console . log ( `You: ${ text } ` ) ;
74+ } catch ( error ) {
75+ console . error ( `Publish failed: ${ error } ` ) ;
76+ }
7777}
7878
7979// Example: publish a message
80- const text_message = " Hello, world!" ;
80+ const text_message = ' Hello, world!' ;
8181publishMessage ( text_message ) ;
8282
8383// snippet.end
0 commit comments