</> Code samples - xCams

We take great pride in following best practices while coding so that the end result is consistent, descriptive, and easy to interpret.

Hence building on top of the script you purchase from us is easy. You can always customize things yourself or reach out to us and let us bring your vision to life.

Feel free to check out some example code snippets below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 import { Controller, Injectable, UseGuards, Body, Post, HttpCode, HttpStatus, UsePipes, ValidationPipe, Put, Get, Param, Query, Request, UseInterceptors, Res, HttpException } from '@nestjs/common'; import { Response, Request as Req } from 'express'; import { DataResponse, PageableData, getConfig, EntityNotFoundException, QueueEventService, QueueEvent } from 'src/kernel'; import { AuthService } from 'src/modules/auth/services'; import { Roles, CurrentUser } from 'src/modules/auth/decorators'; import { UserInterceptor } from 'src/modules/auth/interceptors'; import { RoleGuard, AuthGuard } from 'src/modules/auth/guards'; import { FileUploadInterceptor, FileUploaded, FileDto } from 'src/modules/file'; import { UserDto } from 'src/modules/user/dtos'; import { FavouriteService } from 'src/modules/favourite/services'; import { SettingService } from 'src/modules/settings'; import { SETTING_KEYS } from 'src/modules/settings/constants'; import { CountryService } from 'src/modules/utils/services'; import { DELETE_FILE_TYPE, FileService, FILE_EVENT, MEDIA_FILE_CHANNEL } from 'src/modules/file/services'; import { omit } from 'lodash'; import { EXCLUDE_FIELDS } from 'src/kernel/constants'; import { AccountNotFoundxception } from 'src/modules/user/exceptions'; import { PasswordIncorrectException } from 'src/modules/auth/exceptions'; import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; import { PerformerBroadcastSetting } from '../payloads/performer-broadcast-setting.payload'; import { PERFORMER_STATUSES } from '../constants'; import { PerformerDto, IPerformerResponse, BlockSettingDto } from '../dtos'; import { PerformerUpdatePayload, PerformerSearchPayload, PerformerStreamingStatusUpdatePayload, BlockSettingPayload, DefaultPricePayload } from '../payloads'; import { PerformerService, PerformerSearchService } from '../services'; @Injectable() @Controller('performers') @ApiTags('Performer') export class PerformerController { constructor( private readonly authService: AuthService, private readonly performerService: PerformerService, private readonly performerSearchService: PerformerSearchService, private readonly favoriteService: FavouriteService, private readonly settingService: SettingService, private readonly countryService: CountryService, private readonly fileService: FileService, private readonly queueEventService: QueueEventService ) {} @Get('/me') @HttpCode(HttpStatus.OK) // @Roles('performer') // @UseGuards(RoleGuard) async me(@Request() request: Req): Promise<DataResponse<IPerformerResponse>> { const jwtToken = request.headers.authorization; const performer = await this.authService.getSourceFromJWT(jwtToken); if (!performer || performer.status !== PERFORMER_STATUSES.ACTIVE) { throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED); } const result = await this.performerService.getDetails( performer._id, jwtToken ); return DataResponse.ok(new PerformerDto(result).toResponse(true)); } @Get('/search') @HttpCode(HttpStatus.OK) @UseInterceptors(UserInterceptor) @UsePipes(new ValidationPipe({ transform: true })) async usearch( @Query() req: PerformerSearchPayload, @CurrentUser() user: UserDto, @Request() request: Req ): Promise<DataResponse<PageableData<IPerformerResponse>>> { const query = { ...req }; // only query activated performer, sort by online time query.status = PERFORMER_STATUSES.ACTIVE; let ipClient = request.headers['x-forwarded-for'] || request.connection.remoteAddress; ipClient = Array.isArray(ipClient) ? ipClient.toString() : ipClient; if (ipClient.substr(0, 7) === '::ffff:') { ipClient = ipClient.substr(7); } // const ipClient = '115.75.211.252'; const whiteListIps = ['127.0.0.1', '0.0.0.1']; let userCountry = null; let countryCode = null; if (whiteListIps.indexOf(ipClient) === -1) { userCountry = await this.countryService.findCountryByIP(ipClient); if ( userCountry && userCountry.status === 'success' && userCountry.countryCode ) { countryCode = userCountry.countryCode; } } const data = await this.performerSearchService.advancedSearch( query, user, countryCode ); return DataResponse.ok({ total: data.total, data: data.data }); } @Put('/') @Roles('performer') @UseGuards(RoleGuard) @ApiSecurity('authorization') @ApiOperation({ summary: 'Update Performer'}) async updatePerformer( @CurrentUser() currentPerformer: PerformerDto, @Body() payload: PerformerUpdatePayload, @Request() request: Req ): Promise<DataResponse<IPerformerResponse>> { await this.performerService.update( currentPerformer._id, omit(payload, EXCLUDE_FIELDS) ); const performer = await this.performerService.getDetails( currentPerformer._id, request.headers.authorization ); return DataResponse.ok(new PerformerDto(performer).toResponse(true)); } @Get('/:username/view') @UseInterceptors(UserInterceptor) @HttpCode(HttpStatus.OK) async getDetails( @Param('username') performerUsername: string, @Request() req: Req, @CurrentUser() user: UserDto ): Promise<DataResponse<Partial<PerformerDto>>> { let ipClient = req.headers['x-forwarded-for'] || req.connection.remoteAddress; ipClient = Array.isArray(ipClient) ? ipClient.toString() : ipClient; if (ipClient.substr(0, 7) === '::ffff:') { ipClient = ipClient.substr(7); } // const ipClient = '115.75.211.252'; const whiteListIps = ['127.0.0.1', '0.0.0.1']; let userCountry = null; let countryCode = null; if (whiteListIps.indexOf(ipClient) === -1) { userCountry = await this.countryService.findCountryByIP(ipClient); if ( userCountry && userCountry.status === 'success' && userCountry.countryCode ) { countryCode = userCountry.countryCode; } } const performer = await this.performerService.findByUsername( performerUsername, countryCode, user ); if (!performer || performer.status !== PERFORMER_STATUSES.ACTIVE) { throw new EntityNotFoundException(); } if (user) { const favorite = await this.favoriteService.findOne({ favoriteId: performer._id, ownerId: user._id }); if (favorite) performer.isFavorite = true; } const [defaultGroupChatPrice, defaultC2CPrice] = await Promise.all([ this.settingService.getKeyValue(SETTING_KEYS.GROUP_CHAT_DEFAULT_PRICE) || 0, this.settingService.getKeyValue(SETTING_KEYS.PRIVATE_C2C_PRICE) || 0 ]); performer.privateCallPrice = typeof performer.privateCallPrice !== 'undefined' ? performer.privateCallPrice : defaultC2CPrice; performer.groupCallPrice = typeof performer.groupCallPrice !== 'undefined' ? performer.groupCallPrice : defaultGroupChatPrice; return DataResponse.ok(performer.toPublicDetailsResponse()); } @Post('/documents/upload') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UseInterceptors( FileUploadInterceptor('performer-document', 'file', { destination: getConfig('file').documentDir }) ) async uploadPerformerDocument( @FileUploaded() file: FileDto, @CurrentUser() performer: PerformerDto, @Request() request: Req ): Promise<any> { return DataResponse.ok({ ...file, url: `${file.getUrl()}?documentId=${file._id}&token=${ request.headers.authorization }` }); } @Post('/release-form/upload') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UseInterceptors( FileUploadInterceptor('performer-release-form', 'file', { destination: getConfig('file').documentDir }) ) async uploadPerformerReleaseForm( @FileUploaded() file: FileDto, @CurrentUser() performer: PerformerDto, @Request() request: Req ): Promise<any> { return DataResponse.ok({ ...file, url: `${file.getUrl()}?documentId=${file._id}&token=${ request.headers.authorization }` }); } @Post('/avatar/upload') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UseInterceptors( FileUploadInterceptor('avatar', 'avatar', { destination: getConfig('file').avatarDir, generateThumbnail: true, replaceWithThumbail: true, thumbnailSize: getConfig('image').avatar }) ) async uploadPerformerAvatar( @FileUploaded() file: FileDto, @CurrentUser() performer: PerformerDto ): Promise<any> { await this.performerService.updateAvatar(performer._id, file); await this.fileService.addRef(file._id, { itemId: performer._id, itemType: 'performer-avatar' }); await this.queueEventService.publish( new QueueEvent({ channel: MEDIA_FILE_CHANNEL, eventName: FILE_EVENT.FILE_RELATED_MODULE_UPDATED, data: { type: DELETE_FILE_TYPE.FILEID, currentFile: performer.avatarId, newFile: file._id } }) ); return DataResponse.ok({ ...file, url: file.getUrl() }); } @Post('/streaming-status/update') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UsePipes(new ValidationPipe({ transform: true })) async updateStreamingStatus( @CurrentUser() currentPerformer: PerformerDto, @Body() payload: PerformerStreamingStatusUpdatePayload ): Promise<DataResponse<IPerformerResponse>> { await this.performerService.updateSteamingStatus( currentPerformer._id, payload.status || '' ); const performer = await this.performerService.findById( currentPerformer._id ); return DataResponse.ok(new PerformerDto(performer).toResponse(true)); } @Post('/default-price/update') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UsePipes(new ValidationPipe({ transform: true })) async updateDefaultPrice( @CurrentUser() currentPerformer: PerformerDto, @Body() payload: DefaultPricePayload ): Promise<DataResponse<IPerformerResponse>> { await this.performerService.updateDefaultPrice( currentPerformer._id, payload ); const performer = await this.performerService.findById( currentPerformer._id ); return DataResponse.ok(new PerformerDto(performer).toResponse(true)); } @Post('/broadcast-setting/update') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UsePipes(new ValidationPipe({ transform: true })) async updateBroadcastSetting( @CurrentUser() currentPerformer: PerformerDto, @Body() payload: PerformerBroadcastSetting ): Promise<DataResponse<IPerformerResponse>> { await this.performerService.updateBroadcastSetting( currentPerformer._id, payload ); const performer = await this.performerService.findById( currentPerformer._id ); return DataResponse.ok(new PerformerDto(performer).toResponse(true)); } @Get('/documents/auth/check') @HttpCode(HttpStatus.OK) async checkAuth( @Request() request: Req, @Res() response: Response ): Promise<Response> { if (!request.query.token) { return response.status(HttpStatus.UNAUTHORIZED).send(); } const user = await this.authService.getSourceFromJWT( request.query.token as string ); if (!user) { return response.status(HttpStatus.UNAUTHORIZED).send(); } const valid = await this.performerService.checkAuthDocument(request, user); return response .status(valid ? HttpStatus.OK : HttpStatus.UNAUTHORIZED) .send(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 /* eslint-disable camelcase */ import React, { PureComponent } from 'react'; import Head from 'next/head'; import { Row, Col, Button, message, ButtonProps } from 'antd'; import { connect } from 'react-redux'; import { IPerformer, IUser } from 'src/interfaces'; import { messageService, streamService } from 'src/services'; import { LivePublisher } from '@components/streaming/publisher'; import { SocketContext, Event } from 'src/socket'; import { getStreamConversationSuccess, loadStreamMessages, resetStreamMessage, resetAllStreamMessage, resetStreamConversation } from '@redux/stream-chat/actions'; import { updateStreamingStatus } from '@redux/performer/actions'; import { WEBRTC_ADAPTOR_INFORMATIONS } from 'src/antmedia/constants'; import ChatBox from '@components/stream-chat/chat-box'; import UpdateSatusForm from '@components/performer/streaming-status-update-form'; import Router from 'next/router'; import { getResponseError } from '@lib/utils'; import './index.less'; // eslint-disable-next-line no-shadow enum EVENT_NAME { ROOM_INFORMATIOM_CHANGED = 'public-room-changed', USER_LEFT_ROOM = 'USER_LEFT_ROOM' } interface P { resetStreamMessage: Function; resetAllStreamMessage: Function; getStreamConversationSuccess: Function; loadStreamMessages: Function; updateStreamingStatus: Function; activeConversation: any; performer: IPerformer; updating: boolean; updateSuccess: boolean; updateError: any; resetStreamConversation: Function; } interface S { loading: boolean; fetching: boolean; initialized: boolean; publish_started: boolean; total: number; members: IUser[]; } class PerformerLivePage extends PureComponent<P, S> { static authenticate = true; private publisherRef: any; private btnRef: React.RefObject<HTMLElement> = React.createRef(); private socket: SocketIOClient.Socket; constructor(props: P) { super(props); this.state = { loading: false, fetching: false, initialized: false, publish_started: false, total: 0, members: [] }; } componentDidMount() { this.socket = this.context; this.joinPublicRoom(); window.addEventListener('beforeunload', this.onbeforeunload); Router.events.on('routeChangeStart', this.onbeforeunload); } componentDidUpdate(prevProps: P) { const { updateSuccess, updateError } = this.props; if (prevProps.updateSuccess !== updateSuccess && updateSuccess) { message.success('Update Status Success.'); } if (prevProps.updateError !== updateError && updateError) { message.error(getResponseError(updateError)); } } componentWillUnmount() { window.removeEventListener('beforeunload', this.onbeforeunload); Router.events.off('routeChangeStart', this.onbeforeunload); } handler({ total, members, conversationId }) { const { activeConversation } = this.props; if (activeConversation?.data?._id === conversationId) { this.setState({ total, members }); } } handleUpdateStatusForm(data) { const { updateStreamingStatus: dispatchUpdateStreamingStatus } = this.props; dispatchUpdateStreamingStatus(data); } onbeforeunload = () => { this.leavePublicRoom(); }; start() { this.publisherRef && this.publisherRef.start(); } stop() { const { initialized, publish_started } = this.state; if (!initialized || !publish_started) { window.location.reload(); return; } if (window.confirm('Are you sure want to stop this live streaming!')) { window.location.reload(); } } async callback(info: WEBRTC_ADAPTOR_INFORMATIONS) { const { activeConversation } = this.props; if (activeConversation && activeConversation.data) { this.socket = this.context; if (info === WEBRTC_ADAPTOR_INFORMATIONS.INITIALIZED) { this.setState({ initialized: true }); try { this.setState({ loading: true }); const resp = await streamService.goLive(); this.publisherRef && this.publisherRef.publish(resp.data.sessionId); } catch (e) { const error = await Promise.resolve(e); message.error(getResponseError(error)); this.setState({ loading: false }); } } else if (info === WEBRTC_ADAPTOR_INFORMATIONS.PUBLISH_STARTED) { this.setState({ publish_started: true, loading: false }); this.socket.emit('public-stream/live', { conversationId: activeConversation.data._id }); } else if (info === WEBRTC_ADAPTOR_INFORMATIONS.PUBLISH_FINISHED) { this.setState({ loading: false, publish_started: false }); } else if (info === WEBRTC_ADAPTOR_INFORMATIONS.CLOSED) { this.setState({ loading: false, initialized: false, publish_started: false }); } } } async joinPublicRoom() { const { loadStreamMessages: dispatchLoadStreamMessages, getStreamConversationSuccess: dispatchGetStreamConversationSuccess } = this.props; try { this.setState({ fetching: true }); const resp = await streamService.goLive(); const { conversation } = resp.data; if (conversation && conversation._id) { // this.publisherRef && this.publisherRef.start(); dispatchGetStreamConversationSuccess({ data: conversation }); dispatchLoadStreamMessages({ conversationId: conversation._id, limit: 25, offset: 0, type: conversation.type }); this.socket = this.context; this.socket && this.socket.emit('public-stream/join', { conversationId: conversation._id }); } } catch (e) { const error = await Promise.resolve(e); message.error(getResponseError(error)); } finally { this.setState({ fetching: false }); } } leavePublicRoom() { const { activeConversation, resetStreamMessage: dispatchResetStreamMessage, resetStreamConversation: dispatchResetStreamConversation } = this.props; if (activeConversation && activeConversation.data) { const conversation = { ...activeConversation.data }; this.socket && this.socket.emit('public-stream/leave', { conversationId: conversation._id }); dispatchResetStreamMessage(); dispatchResetStreamConversation(); } if (this.btnRef.current) { this.btnRef.current.remove(); } } userLeftRoomHandle({ username, conversationId }) { const { activeConversation } = this.props; if (activeConversation?.data?._id === conversationId) { const { total, members } = this.state; this.setState({ total: total - 1, members: members.filter((m) => m.username !== username) }); } } async removeAllMessage() { const { activeConversation, performer, resetAllStreamMessage: dispatchResetAllMessage } = this.props; if ( !activeConversation.data || performer._id !== activeConversation.data.performerId ) { return; } try { if (!window.confirm('Are you sure you want to remove chat history?')) { return; } await messageService.deleteAllMessageInConversation( activeConversation.data._id ); dispatchResetAllMessage({ conversationId: activeConversation.data._id }); } catch (e) { const error = await Promise.resolve(e); message.error(getResponseError(error)); } } render() { const { performer, activeConversation, updating } = this.props; const { loading, initialized, publish_started, members, total, fetching } = this.state; const btnProps: ButtonProps & React.RefAttributes<HTMLElement> = { ref: this.btnRef, loading, disabled: fetching, block: true }; if (initialized && publish_started) { btnProps.type = 'text'; btnProps.style = { marginBottom: 10, background: 'black', color: 'white' }; btnProps.children = 'Stop broadcasting'; btnProps.onClick = this.stop.bind(this); } else { btnProps.type = 'primary'; btnProps.style = { marginBottom: 10 }; btnProps.children = 'Start broadcasting'; btnProps.onClick = this.start.bind(this); } return ( <> <Head> <title>Go Live</title> </Head> <Event event={EVENT_NAME.ROOM_INFORMATIOM_CHANGED} handler={this.handler.bind(this)} /> <Event event={EVENT_NAME.USER_LEFT_ROOM} handler={this.userLeftRoomHandle.bind(this)} /> <Row> <Col xs={24} sm={24} md={12}> <UpdateSatusForm status={performer.streamingTitle} updating={updating} submit={this.handleUpdateStatusForm.bind(this)} /> <Button {...btnProps} /> <LivePublisher ref={(ref) => { this.publisherRef = ref; }} onChange={this.callback.bind(this)} configs={{ debug: true, bandwidth: 900, localVideoId: 'publisher' }} /> </Col> <Col xs={24} sm={24} md={12}> <ChatBox {...this.props} members={members} totalParticipant={total} currentPerformer={performer} /> {activeConversation?.data && ( <div style={{ margin: '10px' }}> <Button type="primary" onClick={this.removeAllMessage.bind(this)} > Clear message history </Button> </div> )} </Col> </Row> </> ); } } PerformerLivePage.contextType = SocketContext; const mapStateToProps = (state) => ({ performer: state.performer.current, updating: state.performer.updating, updateSuccess: state.performer.updateSuccess, updateError: state.performer.updateError, activeConversation: state.streamMessage.activeConversation }); const mapDispatchs = { updateStreamingStatus, getStreamConversationSuccess, loadStreamMessages, resetStreamMessage, resetAllStreamMessage, resetStreamConversation }; export default connect(mapStateToProps, mapDispatchs)(PerformerLivePage);