0

How to enable websocket option in Azure bot service? There is a websocket option available in the app service application settings but it is missing in bot service.

I'm running a nodejs bot with sample template and added websocket.

"use strict";
var builder = require("botbuilder");
var botbuilder_azure = require("botbuilder-azure");
var path = require('path');
var useEmulator = (process.env.NODE_ENV == 'development');
var WebSocket = require('ws');
var wss = new WebSocket.Server({ port: process.env.PORT || 8080 });

var connector = useEmulator ? new builder.ChatConnector() : new botbuilder_azure.BotServiceConnector({
    appId: process.env['MicrosoftAppId'],
    appPassword: process.env['MicrosoftAppPassword'],
    stateEndpoint: process.env['BotStateEndpoint'],
    openIdMetadata: process.env['BotOpenIdMetadata']
});

var bot = new builder.UniversalBot(connector);
bot.localePath(path.join(__dirname, './locale'));

bot.dialog('/', function (session) {
    session.send('You said ' + session.message.text);
});

if (useEmulator) {
    var restify = require('restify');
    var server = restify.createServer();
    server.listen(3978, function() {
        console.log('test bot endpont at http://localhost:3978/api/messages');
    });
    server.post('/api/messages', connector.listen());    
} else {
    module.exports = { default: connector.listen() }
}

This is the client side code

var ws = new WebSocket('ws://localhost:8080');
ws.onopen = function() {
   console.log('connected');
};
ws.onclose = function() {
   console.log('disconnected');
};
ws.send('test message from websocket client')

This is working perfectly in the local emulator but I'm getting 503 response code when I tried to connect the azure server

var ws = new WebSocket('ws://mybotname.azurewebsites.net');

Also I tried with port 8080 then I'm getting connection timeout error.

OmG
  • 18,337
  • 10
  • 57
  • 90
rebornx
  • 407
  • 6
  • 21
  • Have you checked out the DirectLineWebSocketSamples https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-DirectLineWebSockets – Eric Dahlvang Aug 20 '17 at 19:25
  • @EricDahlvang Yes, I created this after seeing that directline websocket sample. – rebornx Aug 21 '17 at 15:24
  • 1
    @Rebornx The Azure Bot Service is a "serverless" Azure Functions app. If you want to run a websocket server, use Azure App Service. – nwxdev Aug 21 '17 at 21:02
  • 1
    The sample referenced connects to the direct line via web sockets, but the bot has no web socket server. The client receives messages via the socket, but doesn't use sockets to send messages. There is always a channel between your bot and the client. This is an important architectural point to grasp when understanding how use the bot framework. – Eric Dahlvang Aug 21 '17 at 23:56
  • Like @EricDahlvang says: You don't need a web socket to connect to a bot. Microsoft provides the connection for Skype, iMessage etc. and the messages just arrive in the callback to the UniversalBot function. So what are you actually trying to do? Are you trying to connect to a DirectLine app or to a Bot? – Jerry Jeremiah Sep 25 '17 at 21:43

0 Answers0