Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Pusher adapter nodejs examples #102

Open
wants to merge 18 commits into
base: pusher-adapter-nodejs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
71 changes: 71 additions & 0 deletions client-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
console.warn("\n1. Make sure server is running i.e. `npm run server`"+
"\n2.Run two or more instances of client-events, one for sending and others for receiving client events")
console.warn("\nOnly compatible with unix/bash shell, git bash can be used in windows")

const Pusher = require('pusher-js');

const pusherRealtime = new Pusher('appid.keyid', { // replace with first part of API key before :
wsHost: 'realtime-pusher.ably.io',
wsPort: 443,
disableStats: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@sacOO7 sacOO7 Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I am not sure what it does .... but when I tested it, everything seem to work. Is it supposed to be removed?

useTLS: true,
authEndpoint: 'http://localhost:5000/ably/auth', // deprecated
});

pusherRealtime.connection.bind('connected', () => {
console.log('connected');
});

pusherRealtime.connection.bind('disconnected', () => {
console.log('disconnected');
});

pusherRealtime.connection.bind("error", (error) => {
console.error(error);
});

pusherRealtime.bind_global((eventName, data)=> {
console.log("Global eventName-" + eventName + " data-" + JSON.stringify(data));
});

const alpha = pusherRealtime.subscribe('private-alpha');
alpha.bind("pusher:subscription_succeeded", () => {
console.log('subscribed to alpha')
});
alpha.bind("pusher:subscription_error", (err) => {
console.error('alpha subscription error', err)
});
alpha.bind_global((eventName, data)=> {
console.log("alpha :: eventName-" + eventName + " data-" + JSON.stringify(data));
});

const beta = pusherRealtime.subscribe('private-beta');
beta.bind("pusher:subscription_succeeded", () => {
console.log('subscribed to beta')
});
beta.bind("pusher:subscription_error", (err) => {
console.error('beta subscription error', err)
});
beta.bind_global((eventName, data)=> {
console.log("beta :: eventName-" + eventName + " data-" + JSON.stringify(data));
});


console.log("\n1.Press A<enter> to send message via alpha\n2.Press B<enter> to send message to beta\nPress Q to quit");
process.stdin.resume();
process.stdin.on('data', function (chunk) {
switch(chunk.toString()) {
case "a\n":
case "A\n":
alpha.trigger('client-event', { 'msg': 'Hi from alpha' }); // https://pusher.com/docs/channels/using_channels/events/#eventname-1035650223
break;
case "b\n":
case "B\n":
beta.trigger('client-event', { 'msg': 'Hi from beta' });
break;
case "q\n":
case "Q\n":
pusherRealtime.disconnect();
process.exit();
}
});
75 changes: 75 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
console.warn("\n1. Make sure server is running i.e. `npm run server`")
const Pusher = require('pusher-js');

const pusherRealtime = new Pusher('appid.keyid', { // replace with first part of API key before :
wsHost: 'realtime-pusher.ably.io',
wsPort: 443,
disableStats: false,
useTLS: true,
authEndpoint: 'http://localhost:5000/ably/auth', // deprecated
});

pusherRealtime.connection.bind('connected', () => {
console.log('connected');
});

pusherRealtime.connection.bind('disconnected', () => {
console.log('disconnected');
});

pusherRealtime.connection.bind("error", (error) => {
console.error(error);
});

pusherRealtime.bind_global((eventName, data)=> {
console.log("Global eventName-" + eventName + " data-" + JSON.stringify(data));
});

// Public channel -> Doesn't make any explicit request for authorization
const public_channel = pusherRealtime.subscribe('public-channel');
public_channel.bind("pusher:subscription_succeeded", () => {
console.log('subscribed to public-channel')
});
public_channel.bind("pusher:subscription_error", (err) => {
console.error('public channel subscription error', err)
});
public_channel.bind_global((eventName, data)=> {
console.log("public channel :: eventName-" + eventName + " data-" + JSON.stringify(data));
});

// Private channel -> requests { 'auth' : 'token'} from /ably/auth
const private_channel = pusherRealtime.subscribe('private-channel');
private_channel.bind("pusher:subscription_succeeded", () => {
console.log('subscribed to private-channel')
});
private_channel.bind("pusher:subscription_error", (err) => {
console.error('private channel subscription error', err)
});
private_channel.bind_global((eventName, data)=> {
console.log("private channel :: eventName-" + eventName + " data-" + JSON.stringify(data));
});

// Presence channel -> requests { 'auth' : 'token', 'channelData' : {'userId': '', userInfo: ''}} from /ably/auth
const presence_channel = pusherRealtime.subscribe('presence-channel');
presence_channel.bind("pusher:subscription_succeeded", (members) => {
console.log('\n###subscribed to presence-channel###')
console.log(`members count :: ${members.count}`)
members.each((member) => {
console.log(member.id, member.info)
});
console.log('#########\n')
});
presence_channel.bind("pusher:member_added", (member) => {
console.log('\nmember added', member);
console.log('updated members', presence_channel.members.count)
});
presence_channel.bind("pusher:member_removed", (member) => {
console.log('\nmember removed', member)
console.log('updated members', presence_channel.members.count)
});
presence_channel.bind("pusher:subscription_error", (err) => {
console.error('presence channel subscription error', err)
});
presence_channel.bind_global((eventName, data)=> {
console.log("presence channel :: eventName-" + eventName + " data-" + JSON.stringify(data));
});
13 changes: 8 additions & 5 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
console.warn("Only compatible with unix/bash shell, git bash can be used in windows")
const Ably = require("ably");
const Pusher = require("pusher");

Expand All @@ -24,9 +25,9 @@ ablyChannel.subscribe(function(message) {
console.log('Ably client received a message: ' + message.name + ', data: ' + JSON.stringify(message.data));
});

/* PUSHER CLIENT LIB */
/* Instance the Pusher node library */
const pusher = new Pusher({
/* PUSHER SERVER LIB */
/* Initiate the Pusher node library */
const pusherRest = new Pusher({
appId : APP_ID,
key : KEY_NAME,
secret : KEY_SECRET,
Expand All @@ -35,14 +36,16 @@ const pusher = new Pusher({
});


console.log("Press p<enter> to publish a message with the Pusher nodejs client library, and see it be received by the Ably client library. Press q<enter> to quit.");
console.log("Press p<enter> to publish a message with the Pusher nodejs library, and see it be received by the Ably client library. Press q<enter> to quit.");
process.stdin.resume();
process.stdin.on('data', function (chunk) {
switch(chunk.toString()) {
case "p\n":
pusher.trigger(pusherChannelName, 'eventName', { 'Some': 'JSON data' });
case "P\n":
pusherRest.trigger(pusherChannelName, 'eventName', { 'Some': 'JSON data' });
break;
case "q\n":
case "Q\n":
ably.close();
process.exit();
}
Expand Down
Loading