Last active
January 12, 2021 15:00
-
-
Save chriship/0e11e830c2eb4d1f7b4a05b6574f7092 to your computer and use it in GitHub Desktop.
Node app to connect to laravel websockets server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Pusher = require('pusher-js'); | |
let connected = false | |
let pusher = new Pusher('websocket_test', { | |
wsHost: '192.168.1.78', | |
wsPort: 6001, | |
wssPort: 6001, | |
wsPath: null, | |
disableStats: true, | |
authEndpoint: 'http://192.168.1.78:8000/laravel-websockets/auth', | |
auth: { | |
headers: { | |
'X-CSRF-Token': "KnaeYpl44EOEnZkXDHQLjZcvVj3eP3C2WfaCAAoR", | |
'X-App-ID': 'websocket_test' | |
} | |
}, | |
enabledTransports: ['ws', 'wss', 'flash'] | |
}); | |
pusher.connection.bind('connected', () => { | |
connected = true; | |
console.log('connected') | |
}); | |
pusher.connection.bind('disconnected', () => { | |
connected = false; | |
console.log('disconnected') | |
}); | |
pusher.connection.bind('error', event => { | |
console.log('error', event) | |
connected = false; | |
}); | |
subscribeToAllChannels(); | |
function subscribeToAllChannels() { | |
[ | |
'disconnection', | |
'connection', | |
'vacated', | |
'occupied', | |
'subscribed', | |
'client-message', | |
'api-message', | |
].forEach(channelName => subscribeToChannel(channelName)) | |
} | |
function subscribeToChannel(channel) { | |
pusher.subscribe('private-websockets-dashboard-' + channel) | |
.bind('log-message', (data) => { | |
console.log(data) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment