Issues with websocket connection for @slack/socket-mode
See original GitHub issueDescription
Hi team, I am reaching out per directions from Slack support.
Overview:
My team is having some interesting issues with socket-mode when we are running it using the node.js 12 “@slack/socket-mode” package (v1.1.0) Per the DEBUG level logs we have enabled we do not see any disconnects, disconnect events sent, etc. The container that has this service is up and running the whole time so per the logs nothing is unusual. However we have noticed that randomly we will appear to have no events coming in. During this “down time” window we had sent multiple events to our app (e.g. slash commands, messages, etc) but nothing was received in our logs and we got dispatch failed errors. I have attached our logic to see if there is anything that stands out but would appreciate some assistance in determining why these random disconnects appear to happen
Deployment Info:
Using @slack/socket-mode v1.1.0 Using node.js 12 Running this in an ECS container running out of AWS container is running behind a corporate proxy and so all calls to slack are made through our aws-proxy. socket-mode implementation simply receives events and then forwards events to a lambda for further processing
Code Sample
// Imports
const {SocketModeClient, LogLevel} = require('@slack/socket-mode');
const ProxyAgent = require('proxy-agent');
const Index = require('./index')
// Exports
module.exports = {
sendEvent,
onEvent
}
console.log(`IS_ECS_CONTAINER = ${process.env.IS_ECS_CONTAINER}`)
// Setting up Socket Mode
const socketModeClient = new SocketModeClient({
appToken: process.env.SOCKET_MODE_APP_TOKEN,
socketMode: true,
logLevel: LogLevel.DEBUG,
autoReconnectEnabled: true,
clientOptions: {agent: new ProxyAgent(process.env.HTTPS_PROXY)}
});
socketModeClient.on('message', async ({event, body, ack}) => {
await onEvent(event, body, ack, 'event', 'message')
});
socketModeClient.on('reaction_added', async ({event, body, ack}) => {
await onEvent(event, body, ack, 'event', 'reaction_added')
});
socketModeClient.on('interactive', async ({event, body, ack}) => {
await onEvent(event, body, ack, 'interactive', 'interactive')
});
socketModeClient.on('slash_commands', async ({event, body, ack}) => {
await onEvent(event, body, ack, 'command', 'slash_commands')
});
socketModeClient.on('app_home_opened', async ({event, body, ack}) => {
await onEvent(event, body, ack, 'event', 'app_home_opened')
});
socketModeClient.on('member_joined_channel', async ({event, body, ack}) => {
await onEvent(event, body, ack, 'event', 'member_joined_channel')
});
socketModeClient.on('disconnect', async ({event, body, ack}) => {
try {
console.log(JSON.stringify(body, null, 4))
await ack();
console.log('Got disconnect event, reconnecting to slack')
await socketModeClient.disconnect()
await socketModeClient.start();
console.log('Socket Mode started')
} catch (e) {
console.error(e)
}
});
(async () => {
try {
await socketModeClient.start();
console.log('Socket Mode started')
} catch (e) {
console.error(e)
}
})();
// Helper Functions
/**
* Forwards event to another lambda or local code
* @param body - json event to send
*/
async function sendEvent(body) {
// send to lambda, this env var should not be on for local dev... unless you have a very specific use case
if (process.env.IS_ECS_CONTAINER) {
// awaited function that forwards the event to a lambda to be processed there (legacy code stuff)
}
// send to local index file
else {
await Index.handler({body})
}
}
/**
* handles what to do on an event, will print its details and then call sendEvent()
* @param event - SocketModeClient param
* @param body - SocketModeClient param, is json
* @param ack - SocketModeClient param, is a function to ack event
* @param resourceType - string which says the resource type Slackline classifies this as
* @param eventType - string of the event type the SocketModeClient "on" func ran on
*/
async function onEvent(event, body, ack, resourceType, eventType) {
try {
console.log(`============= ${eventType} =================\n${JSON.stringify(body, null, 4)}\n==============================\n`)
await ack();
body.resource_type = resourceType
await module.exports.sendEvent(body)
} catch (e) {
console.error(e)
}
}
What type of issue is this? (place an x in one of the [ ])
- bug
- enhancement (feature request)
- question
- documentation related
- testing related
- discussion
Requirements (place an x in each of the [ ])
- I’ve read and understood the Contributing guidelines and have done my best effort to follow them.
- I’ve read and agree to the Code of Conduct.
- I’ve searched for any related issues and avoided creating a duplicate issue.
Bug Report
Filling out the following details about bugs will help us solve your issue sooner.
Packages:
Select all that apply:
-
@slack/web-api -
@slack/events-api -
@slack/interactive-messages -
@slack/rtm-api -
@slack/webhooks -
@slack/oauth -
@slack/socket-mode - I don’t know
Reproducible in:
package version: @slack/socket-mode v1.1.0
node version: 12
Steps to reproduce:
npm installsocket-mode and proxy libs- npm run the code above
- let it run for a while
Expected result:
Connection should be up all the time except for when it gets the disconnect event
Actual result:
Connection is just not being reported as terminating but no events can be received and we get dispatch_failed errors
Attachments:
Logs, screenshots, screencast, sample project, funny gif, etc.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (3 by maintainers)
Top Related StackOverflow Question
Yeah, I mean, the point in my mind is that if the same issue is popping up across various client libs, perhaps it’s a problem actually server-side?
In Python I’ve actually seen it with both the regular SocketModeClient as well as the websocket one – so I guess two different python implementations as well.
As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.